gpt4 book ai didi

java - 如何创建一个像这样的简单 xml 文档

转载 作者:行者123 更新时间:2023-11-30 05:04:11 24 4
gpt4 key购买 nike

我想创建一个如下所示的 xml 文档,但我不知道如何:

<bo type="Employee" id="0000012f41bce2a865f8616b0010007c0008008b">
<username>marv</username>
</bo>

这就是我到目前为止所拥有的,我真的只是对如何添加 username 元素感到困惑:

Element bo = testDoc.createElement("bo");
bo.setAttribute("type", "Employee");
bo.setAttribute("id", emp.getId());

最佳答案

Element bo = testDoc.createElement("bo");
bo.setAttribute("type", "Employee");
bo.setAttribute("id", emp.getId());
//create a username element
Element username = testDoc.createElement("username");
//add a text value to the username element
username.appendChild(testDoc.createTextNode("marv"));
//add the username element as child of bo element
bo.appendChild(username);

关于java - 如何创建一个像这样的简单 xml 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5683852/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com