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>
3 个解决方案
#1
13
XPath will help you to find nodes, but not really append them. 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);
#2
2
The most strait-forward way is that you parse, using Sax or 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.
最直接的方法是使用Sax或Dom将所有文件解析为数据结构,例如A类,其中包含一个B类,其中包含C,D,E类的成员。
And output the data structure back to XML.
并将数据结构输出回XML。
#3
0
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"));
}
}
}
#1
13
XPath will help you to find nodes, but not really append them. 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);
#2
2
The most strait-forward way is that you parse, using Sax or 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.
最直接的方法是使用Sax或Dom将所有文件解析为数据结构,例如A类,其中包含一个B类,其中包含C,D,E类的成员。
And output the data structure back to XML.
并将数据结构输出回XML。
#3
0
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"));
}
}
}