Refine search
public static String getElementText(VTDNav vn) throws NavException { StringBuilder sb = new StringBuilder(); int depth = vn.getCurrentDepth(); int i = vn.getCurrentIndex(); while (vn.getTokenType(i) == VTDNav.TOKEN_STARTING_TAG) i++; while (vn.getTokenDepth(i) >= depth && !(vn.getTokenType(i) == VTDNav.TOKEN_STARTING_TAG && vn.getTokenDepth(i) == depth) && i < vn.getTokenCount()) { if (vn.getTokenType(i) == VTDNav.TOKEN_CHARACTER_DATA || vn.getTokenType(i) == VTDNav.TOKEN_CDATA_VAL) sb.append(vn.toString(i)); i++; } return sb.toString(); }
/** * New in v2.12 * Dump an element fragment of xml into an output stream * @param os * @throws NavException * @throws IOException */ final public void dumpFragment(OutputStream os) throws NavException, IOException{ long l = getElementFragment(); dumpFragment(l,os); }
VTDGen vg = new VTDGen(); if (vg.parseFile("c://xml//alex.txt", true)){ VTDNav vn = vg.getNav(); AutoPilot ap = new AutoPilot(vn); ap.selectXPath("//L/D"); int i=-1; while((i=ap.evalXPath())!=-1){ long l = vn.getContentFragment(); System.out.println(" -==> "+ vn.toString((int )l, (int)(l>>32))); } }
private int getStringVal(VTDNav vn,int i){ int i1,t = vn.getTokenType(i); if (t == VTDNav.TOKEN_STARTING_TAG){ i1 = vn.getText(); return i1; } else if (t == VTDNav.TOKEN_ATTR_NAME || t == VTDNav.TOKEN_ATTR_NS || t==VTDNav.TOKEN_PI_NAME ) return i+1; else return i; }
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())); } }
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();
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(vn); ap.selectXPath("/users/user/firstName"); int i=ap.evalXPath(); if (i!=1){ long l= vn.getElementFragment(); System.out.println(" the segment is "+ vn.toString((int)l,(int)(l>>32))); } } }
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"))); } }
final VTDGen vg = new VTDGen(); vg.parseFile("/path/to/file.xml", false); final VTDNav vn = vg.getNav(); final AutoPilot ap = new AutoPilot(vn); ap.selectXPath("/Example/Entry"); int count = 1; while (ap.evalXPath() != -1) { System.out.println("Inside Entry: " + count); //move to n1 child vn.toElement(VTDNav.FIRST_CHILD, "n1"); System.out.println("\tn1: " + vn.toNormalizedString(vn.getText())); //move to n2 child vn.toElement(VTDNav.NEXT_SIBLING, "n2"); System.out.println("\tn2: " + vn.toNormalizedString(vn.getText())); //move back to parent vn.toElement(VTDNav.PARENT); count++; }
import com.ximpleware.*; public class deleteNode { 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("/*/*[@id='s2']");// this is the key XPath expression int i=0; while((i=ap.evalXPath())!=-1){ xm.remove(vn.getElementFragment());// remove the node } xm.output("new.xml"); // output to a new document called new.xml } }
public void formCreateXMLRequest(MAMData mamData,Map<String, String> strTobeModified) throws DatatypeConfigurationException, PayPalUserCreationFailedException, ModifyException, TranscodeException, IOException, XPathEvalException, NavException, XPathParseException { VTDGen vg = new VTDGen(); if (!vg.parseFile(mamData.getDirectory() + mamData.getBatchRequest(), true)) return; VTDNav vn = vg.getNav(); XMLModifier xm = new XMLModifier(vn); AutoPilot ap = new AutoPilot(vn); Set<String> xpathkeys= strTobeModified.keySet(); for(String xpath : xpathkeys) { ap.selectXPath(xpath); while((ap.evalXPath()) != -1) { int p = vn.getText(); xm.updateToken(p, strTobeModified.get(xpath)); } xm.output(mamData.getDirectory()+mamData.getBatchRequest()); } }
import java.io.FileOutputStream; import com.ximpleware.*; public class split { public static void main(String[] args) throws Exception { VTDGen vg = new VTDGen(); if (vg.parseHttpUrl("c:\\xml\\input.xml", true)) { VTDNav vn = vg.getNav(); AutoPilot ap = new AutoPilot(vn); ap.selectXPath("/records/record"); int i=-1,j=0; while ((i = ap.evalXPath()) != -1) { long l=vn.getElementFragment(); (new FileOutputStream("out"+j+".xml")).write(vn.getXML().getBytes(), (int)l,(int)(l>>32)); j++; } } } }
final protected void toRawString(StringBuilder sb, int index) throws NavException { int type = getTokenType(index); int len; if (type == TOKEN_STARTING_TAG || type == TOKEN_ATTR_NAME || type == TOKEN_ATTR_NS) len = getTokenLength2(index) & 0xffff; else len = getTokenLength2(index); int offset = getTokenOffset(index); toRawString(offset, len,sb); } /**
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"); } }
import com.ximpleware.*; public class getContent { public static void main(String s[]) throws VTDException{ VTDGen vg = new VTDGen(); if (!vg.parseFile("C:\\Users\\HC\\Desktop\\dataset\\book.xml", false)) return; VTDNav vn = vg.getNav(); if (vn.toElement(VTDNav.FIRST_CHILD, "book")){ long l = vn.getContentFragment(); System.out.println( "book content ==> "); System.out.println(vn.toString((int)l, (int)(l<<32))); } } }
protected String moveToNextStartingTag() throws DocumentParsingException { try { int i = vn.getCurrentIndex(); int tokenType = vn.getTokenType(i); while (tokenType != VTDNav.TOKEN_STARTING_TAG && i < vn.getTokenCount()) ++i; vn.recoverNode(i); return vn.toString(vn.getCurrentIndex()); } catch (NavException e) { throw new DocumentParsingException(e); } }
/** * * @param l * @throws ModifyException */ private void insertEndingTag(long l) throws ModifyException{ int i = md.getCurrentIndex(); int offset = md.getTokenOffset(i); int length = md.getTokenLength(i)&0xffff; byte[] xml = md.getXML().getBytes(); if (md.encoding <VTDNav.FORMAT_UTF_16BE ) insertBytesAt((int)l,xml,offset,length); else insertBytesAt((int)l, xml, offset<<1, length<<1); }
/** * This method will first call getCurrentIndex() to get the cursor index value * then insert the byte value of s after the element * @param s the string whose byte content will be inserted into the master document * */ public void insertAfterElement(String s) throws ModifyException,UnsupportedEncodingException,NavException{ int startTagIndex =md.getCurrentIndex(); int type = md.getTokenType(startTagIndex); if (type!=VTDNav.TOKEN_STARTING_TAG) throw new ModifyException("Token type is not a starting tag"); long l = md.getElementFragment(); int offset = (int)l; int len = (int)(l>>32); insertBytesAt(offset+len,s.getBytes(charSet)); }
public final ElementParsingResult parse() throws ElementParsingException { try { beforeParseElement(); checkCursorPosition(); int elementDepth = vn.getTokenDepth(vn.getCurrentIndex()); int startElementIndex = vn.getCurrentIndex(); ElementParsingResult elementParsingResult = createParsingResult(); parseElement(elementParsingResult); if (vn.getCurrentIndex() < startElementIndex) vn.recoverNode(startElementIndex); if (vn.getTokenType(vn.getCurrentIndex()) != VTDNav.TOKEN_STARTING_TAG) { vn.toElement(VTDNav.PARENT); if (getElementEnd() > elementParsingResult.getEnd()) throw new IllegalStateException("Parsed element \"" + elementName + "\" ends at byte " + elementParsingResult.getEnd() + " but VTDNav was positioned after parsing within an element which ends at " + getElementEnd() + ". Each element parser must finish within its element or at a starting tag immediately following the parser's element closing tag."); } int index = findIndexAfterElement(elementDepth, vn.getCurrentIndex()); elementParsingResult.setLastTokenIndex(index); afterParseElement(); return elementParsingResult; } catch (NavException e) { throw new ElementParsingException(e); } }