DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Jimmy has posted 14 posts at DZone. View Full User Profile

VTD-XML Parsing Example

11.29.2009
| 6172 views |
  • submit to reddit
        This example demonstrates the basics of non-extractive XML parsing: how to use VTD-XML to parse an XML file, navigates to a node, and prints out its value.

/*
<ns1:a xmlns:ns1="someURL">
	<ns1:b> hello world! </ns1:b>
</ns1:a>
*/
import com.ximpleware.*;

public class hello_world{
	public static void main(String[] args) throws ParseException,NavException{
		VTDGen vg = new VTDGen();
		if (vg.parseFile("d:/java_tutorial_by_code_examples/1/input.xml",true)){
			VTDNav vn = vg.getNav();
			//toElementNS is the namespace aware version of toElement which navigates the cursor
			if (vn.toElementNS(VTDNav.FIRST_CHILD, "someURL", "b")){
				int i= vn.getText(); // get the VTD record index
				if (i!=-1){
					// convert i into string before printing, 
					// toNormalizedString(i) and toRawString(i) are two other options
					System.out.println("the text node value at "+i+" ==> "+vn.toString(i));
				}
			}
		}
	}
}