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

问题描述

| 我想创建一个看起来像这样的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()方法或类似的方法可以使用。     

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...