将节点附加到现有的 xml-Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1431238/
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 16:32:14  来源:igfitidea点击:

Append node to an existing xml-Java

javaxml

提问by Ajay

I have seen the same question being answered for vb and c#, but i need a Java best solution for appending nodes to an xml. Will xpath help? I have

我已经看到为 vb 和 c# 回答了相同的问题,但我需要一个 Java 最佳解决方案来将节点附加到 xml。xpath 有帮助吗?我有

<A>
  <B>
    <c>1<c/>
    <d>2<d/>
    <e>3<e/>
  </B>
  <B>
    <c>1<c/>
    <d>2<d/>
    <e>3<e/>
  </B>
</A>

Need to append another

需要附加另一个

<B>
   <c>11<c/>
   <d>21<d/>
   <e>31<e/>
</B>

回答by Jon Skeet

XPath will help you to findnodes, but not really appendthem. I don't think you'd find it particularly useful here.

XPath 将帮助您查找节点,但不会真正附加它们。我不认为你会发现它在这里特别有用。

Which XML API are you using? If it's the W3C DOM (urgh) then you'd do something like:

您使用的是哪个 XML API?如果它是 W3C DOM (urgh) 那么你会做这样的事情:

Element newB = document.createElement("B");
Element newC = document.createElement("c");
newC.setTextContent("11");
Element newD = document.createElement("d");
newD.setTextContent("21");
Element newE = document.createElement("e");
newE.setTextContent("31");
newB.appendChild(newC);
newB.appendChild(newD);
newB.appendChild(newE);
document.getDocumentElement().appendChild(newB);

回答by Winston Chen

The most strait-forward way is that you parse, using Saxor Dom, all the files into a data structure, for example an A class which has a B class with members of C,D,E class in your case.

最严格的方法是使用SaxDom 将所有文件解析为一个数据结构,例如一个 A 类,它有一个 B 类,在您的情况下是 C、D、E 类的成员。

And output the data structure back to XML.

并将数据结构输出回 XML。

回答by vtd-xml-author

You may want to use XMLModier of vtd-xml to do it in a cool way, which is to append the byte content directly... You just need to call XMLModier's insertAfterElement()... below is a link to the code sample: Incrementally Modify XML in Java:

你可能想使用 vtd-xml 的 XMLModier 来做一个很酷的事情,就是直接追加字节内容...你只需要调用 XMLModier 的 insertAfterElement()... 下面是代码示例的链接:在 Java 中增量修改 XML

import com.ximpleware.*;
import java.io.*;

public class ModifyXML {
     public static void main(String[] s) throws Exception{
        VTDGen vg = new VTDGen(); // Instantiate VTDGen
        XMLModifier xm = new XMLModifier(); //Instantiate XMLModifier
        if (vg.parseFile("old.xml",false)){
             VTDNav vn = vg.getNav();
             xm.bind(vn);

             // first update the value of attr
             int i = vn.getAttrVal("attr");
             if (i!=-1){
                  xm.updateToken(i,"value");
             }

             // navigate to <a>
            if (vn.toElement(VTDNav.FC,"a")) {
                  // update the text content of <a>
                   i=vn.getText();
                   if (i!=-1){
                      xm.updateToken(i," new content ");
                   }
                   // insert an element before <a> (which is the cursor element)
                   xm.insertBeforeElement("<b/>\n\t");

                   // insert an element after <a> (which is the cursor element)
                   xm.insertAfterElement("\n\t<c/>");
            }

            xm.output(new FileOutputStream("new.xml"));
         }
     }

}