import com.ximpleware.*; public class changeAttrVal { public static void main(String s[]) throws VTDException,java.io.UnsupportedEncodingException,java.io.IOException{ VTDGen vg = new VTDGen(); if (!vg.parseFile("input.xml", false)) return; VTDNav vn = vg.getNav(); AutoPilot ap = new AutoPilot(vn); XMLModifier xm = new XMLModifier(vn); ap.selectXPath("/*/place[@id=\"p14\" and @initialMarking=\"2\"]/@initialMarking"); int i=0; while((i=ap.evalXPath())!=-1){ xm.updateToken(i+1, "499");// change initial marking from 2 to 499 } xm.output("new.xml"); // output to a new document called new.xml } }
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); int i = vn.getAttrVal("attr"); if (i!=-1){ xm.updateToken(i,"value"); if (vn.toElement(VTDNav.FC,"a")) { i=vn.getText(); if (i!=-1){ xm.updateToken(i," new content "); xm.insertBeforeElement("<b/>\n\t"); xm.insertAfterElement("\n\t<c/>"); xm.output(new FileOutputStream("new.xml"));
import com.ximpleware.*; public class insertTextNode { public static void main(String[] s) throws VTDException, Exception { VTDGen vg = new VTDGen(); AutoPilot ap = new AutoPilot(); XMLModifier xm = new XMLModifier(); if (vg.parseFile("input.xml", true)) { VTDNav vn = vg.getNav(); ap.bind(vn); xm.bind(vn); ap.selectXPath("/body/request/location/address"); int i=0; while((i=ap.evalXPath())!=-1){ xm.insertAfterHead("myAddress"); } xm.output("output.xml"); } } }
/** * This method will insert a namespace compensated fragment before the tail of cursor element, * @param ef * @throws ModifyException * @throws NavException */ public void insertBeforeTail(ElementFragmentNs ef) throws ModifyException, NavException{ long i = md.getOffsetBeforeTail(); if (i<0){ //throw new ModifyException("Insertion failed"); insertAfterHead(ef); return; } insertElementFragmentNsAt((int)i, ef); }
/** * This method will insert a segment of the byte array before the tail of cursor element, * @param b * @param offset * @param len * @throws ModifyException * @throws NavException */ public void insertBeforeTail(byte[] b, int offset, int len) throws ModifyException,NavException{ long i = md.getOffsetBeforeTail(); if (i<0){ //throw new ModifyException("Insertion failed"); insertAfterHead(b, offset, len); return; } insertBytesAt((int)i,b,offset, len); } /**
import com.ximpleware.*; import java.io.*; public class removeNode { public static void main(String s[]) throws VTDException, IOException{ VTDGen vg = new VTDGen(); if (!vg.parseFile("input.xml", false)); VTDNav vn = vg.getNav(); AutoPilot ap = new AutoPilot(vn); XMLModifier xm = new XMLModifier(vn); ap.selectXPath("/xml/A/B[C/E='13']"); while (ap.evalXPath()!=-1){ xm.remove(); } xm.output("output.xml"); } }
import com.ximpleware.*; public class removeWS { public static void main(String[] s) throws VTDException, Exception{ VTDGen vg = new VTDGen(); AutoPilot ap = new AutoPilot(); XMLModifier xm = new XMLModifier(); if (vg.parseFile("d:\\xml2\\ws.xml", true)){ VTDNav vn = vg.getNav(); ap.bind(vn); xm.bind(vn); ap.selectXPath("//text()"); int i=-1; while((i=ap.evalXPath())!=-1){ int offset = vn.getTokenOffset(i); int len = vn.getTokenLength(i); long l = vn.trimWhiteSpaces((((long)len)<<32)|offset ); System.out.println(" ===> "+vn.toString(i)); System.out.println("len ==>"+len+" new len==>"+ (l>>32)); int nlen = (int)(l>>32); int nos= (int) l; xm.updateToken(i,vn,nos,nlen); } xm.output("d:\\xml2\\new.xml"); } } }
private static void updateAllConfigurations(File pomXml, Map<String, ModelNode> extensionSchemas, int indentationSize) throws Exception { VTDGen gen = new VTDGen(); gen.enableIgnoredWhiteSpace(true); gen.parseFile(pomXml.getAbsolutePath(), true); VTDNav nav = gen.getNav(); XMLModifier mod = new XMLModifier(nav); int textPos = nav.getText(); String jsonConfig = nav.toRawString(textPos); XmlUtil.toIndentedString(xml, indentationSize, nav.getTokenDepth(textPos), pretty); String prettyXml = pretty.toString().substring(indentationSize * nav.getTokenDepth(textPos)); mod.insertAfterElement(prettyXml); mod.remove(); AutoPilot ap = new AutoPilot(nav); ap.selectXPath("//plugin[groupId = 'org.revapi' and artifactId = 'revapi-maven-plugin']/configuration/analysisConfiguration"); while (ap.evalXPath() != -1) { update.call(); mod.output(out);
public static void main(String[] args) throws VTDException,IOException { VTDGen vg = new VTDGen(); AutoPilot ap = new AutoPilot(), ap3=new AutoPilot(), ap4=new AutoPilot(), ap5= new AutoPilot(); ap.selectXPath("/root/a"); while((i=ap5.evalXPath())!=-1){ xm.remove(); while((i=ap.evalXPath())!=-1){ keyName= ap3.evalXPathToString(); vn.push(); while((j=ap4.evalXPath())!=-1){ elementName = vn.toRawString(j); int offset = elementName.indexOf(':'); String newElementName = keyName+"_"+((offset==-1)?elementName: elementName.substring(offset+1)) ; xm.updateElementName(newElementName); ap4.resetXPath(); vn.pop(); xm.output("d:\\xml\\ooo.xml");
VTDGen vg = new VTDGen(); List <String> filter = Arrays.asList("LocalId", "SchoolName", "Principal/FirstName", "PhoneNumberList/PhoneNumber/Number"); if (!vg.parseFile("d:\\xml\\schoolInfo.xml", false)){ return; VTDNav vn = vg.getNav(); FastIntBuffer fib = new FastIntBuffer(); for (i=0;i<vn.getTokenCount();i++){ if (vn.getTokenType(i)==VTDNav.TOKEN_STARTING_TAG){ fib.append(0x1);// b'11 since it is a white list, }else{ if (fib.intAt(j)==0x1){ vn.recoverNode(j); xm.remove(); xm.output("d:\\xml\\newSchoolInfo.xml");
import com.ximpleware.*; import java.io.*; public class removeTag { public static void main(String[] args) throws VTDException,IOException { // TODO Auto-generated method stub VTDGen vg = new VTDGen(); AutoPilot ap=new AutoPilot(); ap.selectXPath("//value"); XMLModifier xm = new XMLModifier(); if (!vg.parseFile("d:\\xml\\tag.xml", false)){ System.out.println("error in xml"); return; } VTDNav vn = vg.getNav(); xm.bind(vn); ap.bind(vn); int i=0; while((i=ap.evalXPath())!=-1){ xm.remove();//remove value fragment long l = vn.getContentFragment(); //replace value with everything it encloses xm.insertBeforeElement(vn.getXML().getBytes(), l); } xm.output("d:\\xml\\newTag.xml"); } }
private void updateAllConfigurationFiles(MavenProject project, Map<String, ModelNode> extensionSchemas, int indentationSize) throws Exception { VTDGen gen = new VTDGen(); gen.enableIgnoredWhiteSpace(true); gen.parseFile(project.getFile().getAbsolutePath(), true); VTDNav nav = gen.getNav(); XMLModifier mod = new XMLModifier(nav); AutoPilot ap = new AutoPilot(nav); ap.resetXPath(); ap.selectXPath(xpath); while (ap.evalXPath() != -1) { int textPos = nav.getText(); String configFile = nav.toString(textPos); mod.updateToken(textPos, newFile.getPath()); mod.output(out);
void updateProjectParentVersion(MavenProject project, Version version) throws MojoExecutionException { try { VTDGen gen = new VTDGen(); gen.enableIgnoredWhiteSpace(true); gen.parseFile(project.getFile().getAbsolutePath(), true); VTDNav nav = gen.getNav(); AutoPilot ap = new AutoPilot(nav); ap.selectXPath("namespace-uri(.)"); String ns = ap.evalXPathToString(); nav.toElementNS(VTDNav.FIRST_CHILD, ns, "parent"); nav.toElementNS(VTDNav.FIRST_CHILD, ns, "version"); int pos = nav.getText(); XMLModifier mod = new XMLModifier(nav); mod.updateToken(pos, version.toString()); try (OutputStream out = new FileOutputStream(project.getFile())) { mod.output(out); } } catch (IOException | ModifyException | NavException | XPathParseException | TranscodeException e) { throw new MojoExecutionException("Failed to update the parent version of project " + project, e); } }
VTDGen gen = new VTDGen(); gen.enableIgnoredWhiteSpace(true); gen.parseFile(project.getFile().getAbsolutePath(), true); VTDNav nav = gen.getNav(); AutoPilot ap = new AutoPilot(nav); XMLModifier mod = new XMLModifier(nav); ap.selectXPath("/project/version"); if (ap.evalXPath() != -1) { int textPos = nav.getText(); mod.updateToken(textPos, version.toString()); } else { mod.insertAfterElement(versionLine.toString()); mod.output(out);
public class simpleSplit { public static void main(String[] s) throws VTDException, IOException{ VTDGen vg = new VTDGen(); if (!vg.parseFile("d:\\xml\\message.xml", true)) //namespace awareness disabled return; VTDNav vn = vg.getNav(); AutoPilot ap = new AutoPilot(vn), ap2=new AutoPilot(vn); XMLModifier xm = new XMLModifier(vn); ap.selectXPath("/messagge/heading/context/forniture/document"); //first xpath FastLongBuffer fib= new FastLongBuffer();// grab all the fragments int i=0;long l1=0,l2=0,l3=0; while((i=ap.evalXPath())!=-1){ fib.append(vn.getElementFragment()); ap2.selectXPath("/messagge/heading/context/forniture"); //second xpath i=ap2.evalXPath(); if(vn.toElement(VTDNav.FIRST_CHILD,"fornitureHeading")){ l1= vn.getElementFragment(); vn.toElement(VTDNav.P); l2 = vn.getContentFragment(); for (int j=0;j<fib.size();j++){ xm.insertAfterHead(vn,l1); xm.removeContent((int)(l2),(int)(l2>>32)); l3= fib.longAt(j); xm.insertBeforeTail(vn,l3); xm.output("d:\\xml\\outMessage"+(j+1)+".xml"); xm.reset();
import com.ximpleware.*; import java.io.*; public class removeAttrNode { public static void main(String[] s) throws VTDException, Exception{ VTDGen vg = new VTDGen(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Payment xmlns=\"http://api.com/schema/store/1.0\"><Store>abc</Store></Payment>"; vg.setDoc(xml.getBytes()); vg.parse(false); // turn off namespace awareness so that VTDNav vn = vg.getNav(); AutoPilot ap = new AutoPilot(vn); XMLModifier xm = new XMLModifier(vn); ap.selectXPath("//@xmlns"); int i=0; while((i=ap.evalXPath())!=-1){ xm.remove(); } xm.output(baos); System.out.println(baos.toString()); } }
String xpath = args[1]; VTDGen vg = new VTDGen(); vg.parseFile(fileName, false); final VTDNav nav = vg.getNav(); AutoPilot ap = new AutoPilot(nav); ap.selectXPath(xpath); XMLModifier mod = new XMLModifier(nav); while (ap.evalXPath() != -1) { mod.remove(); mod.output(fileName);
VTDGen vg = new VTDGen(); // Instantiate VTDGen XMLModifier xm = new XMLModifier(); //Instantiate XMLModifier XMLModifier xm1 = new XMLModifier(); XMLModifier xm2 = new XMLModifier(); vg.parseFile("e://test.xml", true); VTDNav vn = vg.getNav(); vn.toElement(VTDNav.FIRST_CHILD); vn.toElement(VTDNav.NS); if (vn.toElement(VTDNav.FIRST_CHILD, "c")) { xm.bind(vn); xm1.bind(vn); xm.remove(l); // remove the node <c> xm1.remove(); // remove the node <b> to save it in a temp file xm.output(new FileOutputStream("E://new.xml")); // generate the first file xm1.output(new FileOutputStream("E://newonetemp.xml")); xm2.bind(vn); xm2.remove(); // remove the node <b> xm2.output(new FileOutputStream("E://newtwo.xml")); // generate the next file
/** * outAndReparse writes updated XML content into a new byte * array, then parse and return a new VTDNav object * @return VTDNav encapsulating update XML documents * @throws ParseException * @throws IOException * @throws TranscodeException * @throws ModifyException */ public VTDNav outputAndReparse() throws ParseException, IOException,TranscodeException,ModifyException{ XMLByteOutputStream xbos = new XMLByteOutputStream(getUpdatedDocumentSize()); output(xbos); VTDGen vg = new VTDGen(); vg.setDoc(xbos.getXML()); vg.parse(this.md.ns); return vg.getNav(); }
import com.ximpleware.*; public class removeNode { public static void main(String s[]) throws Exception{ VTDGen vg = new VTDGen(); boolean b = vg.parseFile("input.xml", false); if (b==false) return; VTDNav vn = vg.getNav(); XMLModifier xm = new XMLModifier(vn); vn.toElement(VTDNav.FC); // get to A node long l = vn.getContentFragment(); xm.remove(l); xm.output("output.xml"); } }