Over a million developers have joined DZone.

How To Use XPath And Namespaces In VTD-XML?

This example shows you how to use XPath to process an XML document in a namespace aware fashion.

/* Input XML 

	 hello world! 

import com.ximpleware.*; 
public class xpathNs{
        public static void main(String[] args) throws Exception{
            VTDGen vg = new VTDGen();
            AutoPilot ap = new AutoPilot();
            int i;
            // declareXPathNameSpace associates name space with a prefix
            if (vg.parseFile("d:/java_tutorial_by_code_examples/3/input.xml",true)){
                VTDNav vn = vg.getNav();
                // AutoPilot moves the cursor for you, as it returns the index value of the qualified node
                    // notice that i always is equal to vn.getCurrentIndex()!!!
                    System.out.println("the text node index val is "+ 
                        i +" the text string ==>"+vn.toString(i));
                    // below is equivalent to 
                    // vn's cursor is what gets moved by AutoPilot here
                    System.out.println("the text node index val is " + 
                        i + " the text string ==>" + vn.toString(vn.getCurrentIndex()));


Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}