import com.ximpleware.*;
import java.io.*;
// this file incrementally update an XML document
/* Input looks like this:
old content
*/
/* Output looks like this:
new content
*/
public class ModifyXML {
public static void main(String[] s) throws Exception{
// instantiate VTDGen and XMLModifier
VTDGen vg = new VTDGen();
XMLModifier xm = new 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
if (vn.toElement(VTDNav.FC,"a")){
// update the text content of
i=vn.getText();
if (i!=-1){
xm.updateToken(i," new content ");
}
// insert an element before (which is the cursor element)
xm.insertBeforeElement("\n\t");
// insert an element after (which is the cursor element)
xm.insertAfterElement("\n\t");
}
xm.output(new FileOutputStream("new.xml"));
}
}
}