如何使用XMLBeans XmlObject将节点添加到XML

时间:2010-03-25 22:14:07

标签: java xml xmlbeans

我的目标是获取XML字符串并使用XMLBeans XmlObject解析它并添加一些子节点。

这是一个示例文档(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>

基本上,只需添加具有两个子节点<phoneNumbers/><home/>的{​​{1}}节点。

据我所知,

<work/>

谢谢

4 个答案:

答案 0 :(得分:6)

以下是使用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));
    }

}

答案 1 :(得分: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

答案 2 :(得分:0)

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

如果您没有此架构的XSD,可以使用XMLSPY或某些此类工具生成它。

如果您只想要XML操作(即添加节点),您可以尝试其他一些API,例如jdom或xstream或其他类似的东西。

答案 3 :(得分:0)

方法getDomNode()使您可以访问底层的W3C DOM节点。然后,您可以使用W3C Document界面附加孩子。