public static String getXpathValue(String xpath, VTDNav vn) throws XPathParseException { return getXpathValue(xpath, new AutoPilot(vn)); }
AutoPilot ap2 = new AutoPilot(); ap2.selectXPath("blah");
private List<String> getTextPart(VTDNav vn, PartOfDocument part, byte[] identifier) throws XPathParseException, XPathEvalException, NavException { List<String> textParts = new ArrayList<>(); vn.cloneNav(); AutoPilot ap = new AutoPilot(vn); String textPart; if (StringUtils.isBlank(part.getXPath())) throw new IllegalStateException("Document text part with ID " + part.getId() + " has no XPath specified."); ap.selectXPath(part.getXPath()); int i = ap.evalXPath(); if (i < 0 && numNoDocTextFound.incrementAndGet() < 10) LOGGER.debug("no match for xPath " + part.getXPath() + " in document with identifier " + new String(identifier)); else if (numNoDocTextFound.incrementAndGet() == 10) LOGGER.warn("No match for xPath " + part.getXPath() + " in document with identifier " + new String(identifier) + ". This has happened 10 times " + "already (logged on debug level). This message is only displayed once to avoid scrolling."); while (i != -1) { textPart = MapperUtils.getElementText(vn).trim(); textParts.add(textPart); i = ap.evalXPath(); } return textParts; }
/** * Create AutoPilot and declare namespaces on it. * @param xpathExpr xpath expression for the AutoPilot * @return the AutoPilot * @throws XPathParseException */ private AutoPilot acquireAutoPilot(String xpathExpr) throws XPathParseException { AutoPilot ap = compiledXPaths.remove(xpathExpr); if (ap == null) { ap = new AutoPilot(nav); if (config.isNamespaceAware()) { ap.declareXPathNameSpace("xml", "http://www.w3.org/XML/1998/namespace"); // builtin for (Entry<String, String> e: config.getNamespaces().entrySet()) { ap.declareXPathNameSpace(e.getKey(), e.getValue()); } } try { ap.selectXPath(xpathExpr); } catch (XPathParseException e) { throw new RuntimeException("Error in XPath expression " + xpathExpr + " : " + e.getMessage(), e); } } else { ap.resetXPath(); } autoPilotsInUse.put(ap, xpathExpr); return ap; }
/** * Gets attribute map for current element */ private Map<String, String> getAttributes() { navpush(); AutoPilot apAttr = new AutoPilot(nav); apAttr.selectAttr("*"); int i = -1; Map<String, String> attr = new HashMap<>(); try { while ((i = apAttr.iterateAttr()) != -1) { String name = nav.toString(i); String value = nav.toString(i + 1); attr.put(name, value); } } catch (NavException e) { throw new RuntimeException(e); } navpop(); return attr; }
byte[] doc = FileCopyUtils.copyToByteArray(super.createResource("dom03.xml").getInputStream()); VTDGen vtd = new VTDGen(); vtd.setDoc(doc); vtd.parse(true); VTDNav nav = vtd.getNav(); AutoPilot ap = new AutoPilot(); ap.bind(nav); ap.selectElementNS("*", "*"); VtdXmlCurrentState cxp = new VtdXmlCurrentState(); ap.iterate(); assertEquals("/{urn:test:1.0}set/", cxp.currentXPath(nav)[1]); ap.iterate(); assertEquals("/{urn:test:1.0}set/{urn:test:1.0}documents/", cxp.currentXPath(nav)[1]); ap.iterate(); assertEquals("/{urn:test:1.0}set/{urn:test:1.0}documents/{urn:test:1.1}doc/", cxp.currentXPath(nav)[1]); ap.iterate(); assertEquals("/{urn:test:1.0}set/{urn:test:1.0}documents/{urn:test:1.0}doc/", cxp.currentXPath(nav)[1]); ap.iterate(); assertEquals("/{urn:test:1.0}set/{urn:test:1.0}documents/{urn:test:1.2}doc/", cxp.currentXPath(nav)[1]); ap.iterate(); assertEquals("/{urn:test:1.0}set/{urn:test:1.0}documents/", cxp.currentXPath(nav)[1]); ap.iterate(); assertEquals("/{urn:test:1.0}set/documents/", cxp.currentXPath(nav)[1]); assertFalse(ap.iterate());
import com.ximpleware.*; public class concatTest{ public static void main(String s1[]) throws Exception { VTDGen vg= new VTDGen(); String s = "<users><user><firstName>some </firstName><lastName> one</lastName></user></users>"; vg.setDoc(s.getBytes()); vg.parse(false); VTDNav vn = vg.getNav(); AutoPilot ap = new AutoPilot(); ap.selectXPath("concat('good', ' luck')"); System.out.println(" concat output ==>"+ap.evalXPathToString()); ap.selectXPath("concat(/, '')"); ap.bind(vn); System.out.println(" concat output ==>"+ap.evalXPathToString()); } }
import com.ximpleware.*; public class test { public static void main(String[] args) throws Exception { String xpathExpr = "/eventMetrics/day/@totalCount"; VTDGen vg = new VTDGen(); int i = -1; if (vg.parseHttpUrl("http://localhost/test.xml", true)) { VTDNav vn = vg.getNav(); AutoPilot ap = new AutoPilot(); ap.selectXPath(xpathExpr); ap.bind(vn); System.out.println("total count "+(int)ap.evalXPathtoDouble()); } } }
import com.ximpleware.*; public class dumpKeyValue { public static void main(String[] s) throws VTDException{ VTDGen vg = new VTDGen(); AutoPilot ap = new AutoPilot(); ap.selectXPath("//part/*[text()]"); if (!vg.parseFile("d:\\xml\\soap.xml", false)){ System.out.println("invalid xml"); System.exit(0); } VTDNav vn = vg.getNav(); ap.bind(vn); int i=0; while((i=ap.evalXPath())!=-1){ System.out.println(" "+vn.toRawString(i)+" = "+vn.toNormalizedString(vn.getText())); } } }
import com.ximpleware.*; public class dumpKeyValue { public static void main(String[] s) throws VTDException{ VTDGen vg = new VTDGen(); AutoPilot ap = new AutoPilot(); ap.selectXPath("//*[text()]"); if (!vg.parseFile("d:\\xml\\keyValue.xml", false)){ System.out.println("invalid xml"); System.exit(0); } VTDNav vn = vg.getNav(); ap.bind(vn); int i=0; while((i=ap.evalXPath())!=-1){ System.out.println(" "+vn.toRawString(i)+" = "+vn.toNormalizedString(vn.getText())); } } }
VTDGen vg = new VTDGen(); AutoPilot ap = new AutoPilot(); int i; ap.selectXPath("/root/device/modelname"); if (vg.parseFile(PATH_TO_FILE,true)){ VTDNav vn = vg.getNav(); ap.bind(vn); // apply XPath to the VTDNav instance // AutoPilot moves the cursor for you while((i=ap.evalXPath())!=-1){ System.out.println("the value is: " + vn.toNormalizedString(vn.getText())); } }
VTDGen vg = new VTDGen(); AutoPilot ap = new AutoPilot(); int i; ap.selectXPath("/root/element"); if (vg.parseFile(FILE_LOCATION,true)){ VTDNav vn = vg.getNav(); ap.bind(vn); // apply XPath to the VTDNav instance // AutoPilot moves the cursor for you while((i=ap.evalXPath())!=-1){ System.out.println("the attribute index val is "+ i +" the attribute string ==>"+vn.toString(vn.getAttrVal("attribute"))); } }
public String readXpathValue(String dir, String file, String xpath) { String value = null; try{ VTDGen vg = new VTDGen(); int i; AutoPilot ap = new AutoPilot(); ap.selectXPath(xpath); if (vg.parseFile(dir+file, true)) { VTDNav vn = vg.getNav(); ap.bind(vn); //XPath eval returns one node at a time while ((i = ap.evalXPath()) != -1) { value = vn.toString(i); } // ap.resetXPath(); } } catch (Exception e){ System.out.println("Exception Occurred in reading Xpath Value : "+e); } return value; }
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); } }
static VTDGen vg = new VTDGen(); static String inFile = "c:\\xml\\kepler.xml"; static XMLModifier xm = new XMLModifier(); /** * @param args */ public static void main(String[] args) throws ParseException, NavException, XPathParseException, XPathEvalException, Exception{ // TODO Auto-generated method stub int result; AutoPilot ap = new AutoPilot(); ap.selectXPath("//Address[LastName='ghi']/FirstName/text()"); if (vg.parseFile(inFile,true)){ VTDNav vn = vg.getNav(); ap.bind(vn);xm.bind(vn); while((result = ap.evalXPath())!=-1){ //System.out.println(vn.getText() + vn.toString(result)); xm.updateToken(result,"TestValue"); } } xm.output(new FileOutputStream("c:\\xml\\resultbooks.xml")); }
import com.ximpleware.*; public class replaceContent { 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:\\xml\\input2.xml", true)){ VTDNav vn = vg.getNav(); ap.bind(vn); xm.bind(vn); ap.selectXPath("/clients/function/block"); int i=-1; byte[] s1 = ("\r\n\t\t<opster>hello = world</opster>\r\n\t\t"+ "<opster>abc = def</opster>\r\n\t").getBytes(); while((i=ap.evalXPath())!=-1){ xm.insertAfterHead(s1); long l= vn.getContentFragment(); // add new stuff after the starting tag xm.removeContent((int)l, (int)(l>>32)); // remove old stuff } xm.output("d:\\xml\\new.xml"); } } }
@Override public void parseType(ConcreteType concreteType, VTDNav nav, JCas jcas, byte[] identifier, DocumentTextData docText) throws Exception { VTDNav vn = nav.cloneNav(); for (FeatureTemplate featureTemplate : concreteType.getTypeTemplate().getFeatures()) { for (String xPath : featureTemplate.getXPaths()) { AutoPilot ap = new AutoPilot(vn); ap.selectXPath(xPath); while (ap.evalXPath() != -1) { ConcreteFeature concreteFeature = new ConcreteFeature(featureTemplate); parseSingleType(concreteFeature, vn, jcas, identifier, docText); concreteType.addFeature(concreteFeature); } } } }
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"); } } }
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"); } } }
public static void parseStandardFeature(ConcreteType concreteType, VTDNav nav, FeatureTemplate featureTemplate, ConcreteFeature concreteFeature) throws XPathParseException, XPathEvalException, NavException { VTDNav vn = nav.cloneNav(); for (String xpath : featureTemplate.getXPaths()) { AutoPilot ap = new AutoPilot(vn); ap.selectXPath(xpath); int i = ap.evalXPath(); while (i != -1) { String value; if (vn.getTokenType(i) == VTDNav.TOKEN_ATTR_NAME) { value = vn.toString(i + 1); } else { value = MapperUtils.getElementText(vn); } String mappedValue = featureTemplate.getMappedValue(value); concreteFeature.setValue(mappedValue); concreteType.addFeature(concreteFeature); concreteFeature = new ConcreteFeature(featureTemplate); i = ap.evalXPath(); } } }