您可能希望使用XMLMODIER的vtd-xml来以一种很酷的方式来完成它,它直接追加字节内容......您只需调用XMLModier的insertAfterElement()... 下面是一个链接代码示例:Incrementally Modify XML in Java:
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"));
}
}
}
并设置c,d,e值? – Ajay 2009-09-16 06:23:58
编辑...尽管如果您想了解如何在这些元素中创建内容,在问题中这样说会很有用。 – 2009-09-16 06:36:10
抱歉...已经完成了!..感谢您的回复 – Ajay 2009-09-16 06:40:12