static void Main(string[] args) { VTDGen vg = new VTDGen(); int i; AutoPilot ap = new AutoPilot(); ap.selectXPath("/CATALOG/CD[PRICE < 10]"); BookMark bm = new BookMark(); if (vg.parseFile("cd.xml", false)) { VTDNav vn = vg.getNav(); bm.bind(vn); ap.bind(vn); //XPath eval returns one node at a time while ((i = ap.evalXPath()) != -1) { // push the current cursor position //vn.push(); bm.recordCursorPosition(); // equivalent to vn.push(); // get to the first child if (vn.toElement(VTDNav.FIRST_CHILD, "TITLE")) { int j = vn.getText(); if (j != -1) Console.WriteLine(" text node ==>" + vn.toString(j)); } // restore the cursor position //vn.pop(); bm.setCursorPosition(); // equivalent to vn.pop(); } ap.resetXPath(); } }
/// <summary> /// /// Return the byte offset and length of up to i sibling fragments. If /// there is a i+1 sibling element, the cursor element would /// move to it; otherwise, there is no cursor movement. If the cursor isn't /// positioned at an element (due to XPath evaluation), then -1 will be /// returned /// @param i number of silbing elements including the cursor element /// @return a long encoding byte offset (bit 31 to bit 0), length (bit 62 /// to bit 32) of those fragments /// @throws NavException /// </summary> /// <param name="i"></param> /// <returns></returns> public long getSiblingElementFragments(int i) { if (i <= 0) throw new ArgumentException(" # of sibling can be less or equal to 0"); // get starting char offset if (atTerminal == true) return -1L; // so is the char offset int so = getTokenOffset(getCurrentIndex()) - 1; // char offset to byte offset conversion if (encoding >= FORMAT_UTF_16BE) so = so << 1; BookMark bm = new BookMark(this); bm.recordCursorPosition(); while (i > 1 && toElement(VTDNav.NEXT_SIBLING)) { i--; } long l = getElementFragment(); int len = (int)l + (int)(l >> 32) - so; if (i == 1 && toElement(VTDNav.NEXT_SIBLING)) { } else bm.setCursorPosition(); return (((long)len) << 32) | so; }