【问题标题】:How to add a node to XML with XMLBeans XmlObject如何使用 XMLBeans XmlObject 将节点添加到 XML
【发布时间】:2010-03-25 22:14:06
【问题描述】:

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

基本上,只需添加&lt;phoneNumbers/&gt; 节点和两个子节点&lt;home/&gt;&lt;work/&gt;

据我所知,

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

谢谢

【问题讨论】:

    标签: java xml xmlbeans


    【解决方案1】:

    这是一个使用 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));
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      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:我看起来很容易使用!会一直关注的
      • 是的,我也喜欢这个——从未使用过 XOM。
      【解决方案3】:

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

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

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

      【讨论】:

      • 我没有尝试生成 XMLBEANS java 对象。关于从哪里寻找或开始的任何指示?我对用 Java 解析 XML 很陌生。我也会看看 jdom 和 xstream。
      • 如果你清楚地定义你的最终目标,那肯定会有所帮助。是“在现有的 xml 中添加几个节点并输出 xml”吗?
      • 如果您想要简单的操作,请查看此链接exampledepot.com/egs/org.w3c.dom/AddText.html
      • 哦,对不起,是的,我想将 xml 字符串作为输入并输出新的 xml,并将节点添加为字符串。所以输入应该是xml,输出应该是添加了新节点的xml。
      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-25
        • 1970-01-01
        相关资源
        最近更新 更多