问题描述
|
我想创建一个看起来像这样的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);
,创建一个username元素,设置其值,然后使其成为bo元素的子元素。我不知道Java,但是可能有一个AddChild()方法或类似的方法可以使用。