3

我的目标是获取一个 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/><work/>.

据我所知,这是

XmlObject xml = XmlObject.Factory.parse(xmlString);

谢谢

4

4 回答 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));
    }

}
于 2011-05-06T18:50:24.557 回答
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
于 2010-04-27T20:38:30.677 回答
0

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

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

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

于 2010-03-26T12:03:14.067 回答
0

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

于 2012-08-01T12:10:26.773 回答