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/>2 つの子ノード<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からXMLBEANSJavaオブジェクトを生成することを検討しましたか?

このスキーマのXSDがない場合は、XMLSPYまたはそのようなツールを使用して生成できます。

XML操作(つまり、ノードの追加)だけが必要な場合は、jdomやxstreamなどの他のAPIを試すことができます。

于 2010-03-26T12:03:14.067 に答える
0

メソッドgetDomNode()を使用すると、基になる W3C DOM ノードにアクセスできます。次に、W3C Document インターフェイスを使用して子を追加できます。

于 2012-08-01T12:10:26.773 に答える