如何使用XMLBeans XmlObject向XML添加节点

3
我的目标是使用XMLBeans XmlObject解析XML字符串并添加一些子节点。
以下是示例文档(xmlString):
<?xml version="1.0"?>
<rootNode>
 <person>
  <emailAddress>joefoo@example.com</emailAddress>
 </person>
</rootNode>

在添加一些节点后,这是我希望XML文档的样子:

<?xml version="1.0"?>
<rootNode>
 <person>
  <emailAddress>joefoo@example.com</emailAddress>
  <phoneNumbers>
   <home>555-555-5555</home>
   <work>555-555-5555</work>
  <phoneNumbers>
 </person>
</rootNode>

基本上,只需添加一个带有两个子节点和的节点。
这是我到目前为止所做的。
XmlObject xml = XmlObject.Factory.parse(xmlString);

感谢您的选择。
4个回答

7
这里是使用XmlCursor插入新元素的示例。您还可以获取XmlObject的DOM节点并使用这些API。
import org.apache.xmlbeans.*;

/**
 * Adding nodes to xml using XmlCursor.
 * @see http://xmlbeans.apache.org/docs/2.4.0/guide/conNavigatingXMLwithCursors.html
 * @see http://xmlbeans.apache.org/docs/2.4.0/reference/org/apache/xmlbeans/XmlCursor.html
 */
public class AddNodes
{
    public static final String xml =
    "<rootNode>\n" +
    "  <person>\n" +
    "    <emailAddress>joefoo@example.com</emailAddress>\n" +
    "  </person>\n" +
    "</rootNode>\n";

    public static XmlOptions saveOptions = new XmlOptions().setSavePrettyPrint().setSavePrettyPrintIndent(2);

    public static void main(String[] args) throws XmlException
    {
        XmlObject xobj = XmlObject.Factory.parse(xml);
        XmlCursor cur = null;
        try
        {
            cur = xobj.newCursor();
            // We could use the convenient xobj.selectPath() or cur.selectPath()
            // to position the cursor on the <person> element, but let's use the
            // cursor's toChild() instead.
            cur.toChild("rootNode");
            cur.toChild("person");
            // Move to </person> end element.
            cur.toEndToken();
            // Start a new <phoneNumbers> element
            cur.beginElement("phoneNumbers");
            // Start a new <work> element
            cur.beginElement("work");
            cur.insertChars("555-555-5555");
            // Move past the </work> end element
            cur.toNextToken();
            // Or insert a new element the easy way in one step...
            cur.insertElementWithText("home", "555-555-5555");
        }
        finally
        {
            if (cur != null) cur.dispose();
        }

        System.out.println(xobj.xmlText(saveOptions));
    }

}

3

XMLBeans似乎很麻烦,这里提供一种使用XOM的解决方案:

import nu.xom.*;

Builder = new Builder();
Document doc = builder.build(new java.io.StringBufferInputStream(inputXml));
Nodes nodes = doc.query("person");
Element homePhone = new Element("home");
homePhone.addChild(new Text("555-555-5555"));
Element workPhone = new Element("work");
workPhone.addChild(new Text("555-555-5555"));
Element phoneNumbers = new Element("phoneNumbers");
phoneNumbers.addChild(homePhone);
phoneNumbers.addChild(workPhone);
nodes[0].addChild(phoneNumbers);
System.out.println(doc.toXML()); // should print modified xml

非常有趣:我之前不知道XOM,看起来非常容易使用!我会关注它。 - MarcoS
是的,我也喜欢 - 从来没有用过 XOM。 - wsams

0

仅使用XmlObject接口来操作对象可能有些困难。您是否考虑过从此xml生成XMLBEANS java对象?

如果您没有此模式的XSD,可以使用XMLSPY或类似工具生成它。

如果您只想进行XML操作(即添加节点),您可以尝试一些其他API,如jdom或xstream等。


我还没有尝试过生成XMLBEANS Java对象。有什么指针可以提供,告诉我从哪里开始吗?我在使用Java解析XML方面还很新手。我也会看看jdom和xstream。 - wsams
1
如果您清楚地定义了您的最终目标,那肯定会有所帮助。它是“向现有的 XML 添加几个节点并输出 XML”吗? - Kannan Ekanath
1
如果您想进行简单的操作,请查看此链接:http://exampledepot.com/egs/org.w3c.dom/AddText.html - Kannan Ekanath
抱歉,是的,我想将XML字符串作为输入并将添加了节点的新XML作为字符串输出。因此,输入应为XML,输出应为添加了新节点的XML。 - wsams
1
那么这些链接 http://exampledepot.com/egs/org.w3c.dom/AddNode.html 和 http://exampledepot.com/egs/org.w3c.dom/AddText.html 没有解决你的问题吗? - Kannan Ekanath
显示剩余3条评论

0

getDomNode() 方法可以让你访问底层的 W3C DOM 节点。然后,你可以使用 W3C 文档接口来添加子节点。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接