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

发布于 2024-08-26 19:05:04 字数 1088 浏览 3 评论 0原文

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

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

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

这是我希望 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>

基本上,只需添加 节点和两个子节点 <家/><工作/>

这是我目前了解到的情况,

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

谢谢

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

Here's an example document (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 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/>.

This is as far as I've gotten,

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

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

薄荷梦 2024-09-02 19:05:04

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

}

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.

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));
    }

}
錯遇了你 2024-09-02 19:05:04

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

XMLBeans seems like a hassle, here's a solution using 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
绅士风度i 2024-09-02 19:05:04

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

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

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

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?

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

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

分開簡單 2024-09-02 19:05:04

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文