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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2519804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:34:51  来源:igfitidea点击:

How to add a node to XML with XMLBeans XmlObject

javaxmlxmlbeans

提问by wsams

My goal is to take an XML string and parse it with XMLBeans XmlObject and add a few child nodes.

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

Here's an example document (xmlString),

这是一个示例文档(xmlString),

<?xml version="1.0"?>
<rootNode>
 <person>
  <emailAddress>[email protected]</emailAddress>
 </person>
</rootNode>

Here's the way I'd like the XML document to be after adding some nodes,

这是添加一些节点后我希望 XML 文档的方式,

<?xml version="1.0"?>
<rootNode>
 <person>
  <emailAddress>[email protected]</emailAddress>
  <phoneNumbers>
   <home>555-555-5555</home>
   <work>555-555-5555</work>
  <phoneNumbers>
 </person>
</rootNode>

Basically, just adding the <phoneNumbers/>node with two child nodes <home/>and <work/>.

基本上,只需添加<phoneNumbers/>具有两个子节点的节点<home/><work/>.

This is as far as I've gotten,

这是我得到的,

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

Thank you

谢谢

采纳答案by Nathan Hughes

XMLBeans seems like a hassle, here's a solution using XOM:

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

回答by Kevin Krouse

Here is an example of using the XmlCursor to insert new elements. You can also get a DOM Node for an XmlObject and using those APIs.

下面是使用 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>[email protected]</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));
    }

}

回答by Kannan Ekanath

It may be a little difficult to manipulate the objects using just the XmlObject interface. Have you considered generating the XMLBEANS java objects from this xml?

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

If you don't have XSD for this schema you can generate it using XMLSPY or some such tools.

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

If you just want XML manipulation (i.e, adding nodes) you could try some other APIs like jdom or xstream or some such thing.

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

回答by jseba

Method getDomNode()gives you access to the underlying W3C DOM Node. Then you can append childs using W3C Document interface.

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