protected int getElementStart() throws NavException { long elementFragment = vn.getElementFragment(); int offset = (int) elementFragment; return offset; }
protected int getElementEnd() throws NavException { long elementFragment = vn.getElementFragment(); int offset = (int) elementFragment; int length = (int) (elementFragment >> 32); return offset + length; }
/** * 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); }
@Override protected void startDocument() { super.startDocument(); try { long fragment = nav.getElementFragment(); documentByteOffset = (int)fragment; documentLengthBytes = (int)(fragment >> 32); } catch (NavException e) { throw new RuntimeException(e); } lastCharPosition = 0; lastCharPositionByteOffset = documentByteOffset; }
/** * New in v2.12 * Dump an element fragment of xml into an XML file whose name is specified as fileName * @param fileName * @throws NavException * @throws FileNotFoundException * @throws IOException */ final public void dumpFragment(String fileName) throws NavException, FileNotFoundException,IOException{ long l = getElementFragment(); dumpFragment(l,fileName); } /**
protected int getCurrentByteOffset() throws NavException { if (fragPos == FragmentPosition.BEFORE_OPEN_TAG || fragPos == FragmentPosition.AFTER_CLOSE_TAG) { long elFrag = nav.getElementFragment(); int elOffset = (int)elFrag; if (fragPos == FragmentPosition.AFTER_CLOSE_TAG) { int elLength = (int)(elFrag >> 32); return elOffset + elLength; } return elOffset; } long contFrag = nav.getContentFragment(); int contOffset = (int)contFrag; if (fragPos == FragmentPosition.BEFORE_CLOSE_TAG) { int contLength = (int)(contFrag >> 32); return contOffset + contLength; } return contOffset; }
import com.ximpleware.*; import java.io.*; public class splitXML { public static void main(String[] args) throws VTDException, IOException { VTDGen vg = new VTDGen(); if (!vg.parseFile("d:\\xml\\input.xml", false)){ System.out.println("error"); return; } VTDNav vn = vg.getNav(); AutoPilot ap = new AutoPilot(vn); ap.selectXPath("/tag0/*"); int i=0,n=0; FileOutputStream fos =null; while((i=ap.evalXPath())!=-1){ fos = new FileOutputStream("d:\\xml\\output"+(++n)+".xml"); long l = vn.getElementFragment(); fos.write(vn.getXML().getBytes(), (int)l, (int)(l>>32)); fos.close(); } } }
/** * 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)); }
/** * 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)); }
/** * 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)); }
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++; } } } }
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))); } } }
/** * This method will first call getCurrentIndex() to get the cursor index value * then insert the byte array b after the element * @param b * @throws ModifyException * @throws NavException * */ public void insertAfterElement(byte[] b) throws ModifyException,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,b); }
/** * Insert a namespace compensated element after cursor element * @param ef (an ElementFragmentNs object) * @throws ModifyException * @throws NavException * */ public void insertAfterElement(ElementFragmentNs ef) throws ModifyException, 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); insertElementFragmentNsAt(offset + len, ef); }
/** * Insert a namespace compensated element after cursor element * @param ef (an ElementFragmentNs object) * @throws ModifyException * @throws NavException * */ public void insertAfterElement(ElementFragmentNs ef) throws ModifyException, 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); insertElementFragmentNsAt(offset + len, ef); }
/** * Insert a namespace compensated element after cursor element * @param ef (an ElementFragmentNs object) * @throws ModifyException * @throws NavException * */ public void insertAfterElement(ElementFragmentNs ef) throws ModifyException, 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); insertElementFragmentNsAt(offset + len, ef); }
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 } }
/** * This method will first call getCurrentIndex() to get the cursor index value * then insert a segment of the byte array b after the element * @param b * @param contentOffset * @param contentLen * @throws ModifyException * @throws NavException * */ public void insertAfterElement(byte[] b, int contentOffset, int contentLen) throws ModifyException, 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, b, contentOffset, contentLen); }
/** * This method will first call getCurrentIndex() to get the cursor index value * then insert the byte array b after the element * @param b * @throws ModifyException * @throws NavException * */ public void insertAfterElement(byte[] b) throws ModifyException,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,b); }
/** * This method will first call getCurrentIndex() to get the cursor index value * then insert a segment of the byte array b after the element * @param b * @param contentOffset * @param contentLen * @throws ModifyException * @throws NavException * */ public void insertAfterElement(byte[] b, int contentOffset, int contentLen) throws ModifyException, 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, b, contentOffset, contentLen); }