blog.broncotoxique.com

Juste another geek’s website

VTD-XML :: Iterate over block

This post is the result of my research after having difficulties with VTD-XML and iteration over a list of blocks.

Let say you have a XML document like following, and you want to get the value of the entity. It also works for any XPath match, that means you can match an XPath relative to an XPath “Base”.

<?xml version="1.0" encoding="UTF-8"?>
<root>
	<entities>
		<entity>
			<value>val</value>
			<second>sec</second>
		</entity>
		<entity>
			<value>val</value>
			<second>sec</second>
		</entity>
		<entity>
			<value>val</value>
			<second>sec</second>
		</entity>
		<entity>
			<value>val</value>
			<second>sec</second>
		</entity>
		<entity>
			<value>val</value>
			<second>sec</second>
		</entity>
		<entity>
			<value>val</value>
			<second>sec</second>
		</entity>
	</entities>
</root>
private List<Entity> iterateOverEntities(String xml) {
    List<Entity> out = new ArrayList<>();
    
    try {
        VTDGen vg = new VTDGen();
        AutoPilot aph = new AutoPilot();
        vg.setDoc(xml.getBytes());
        vg.parse(false);
        ap.bind(vg.getNav());
        VTDNav vn = aph.getNav();
        String xPathBase = "/root/entities/entity";
        aph.selectXPath(xPathBase);
        while (aph.evalXPath() != -1) {
            vn.push();
            AutoPilot aph1 = new AutoPilot(vn);
            String subXPath = "./value";
            aph1.selectXPath(subXPath);
            String value = aph1.evalXPathToString();
            out.add(new Entity(value));
            vn.pop();
            log.info("Looping");
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
    return out;
}

Following the example here: https://stackoverflow.com/questions/73140467/vtd-xml-select-child-nodes you can do the same on a file stored on a filesystem.

// Source - https://stackoverflow.com/a/73251525
// Posted by technocrat
// Retrieved 2026-03-24, License - CC BY-SA 4.0
public static void main(String[] args) throws XPathParseExceptionHuge, XPathEvalExceptionHuge, NavExceptionHuge, NavException, XPathParseException, XPathEvalException {
	String xpath = "/*/School[starts-with(ScId,'RC')]";
	String xml = "config/school.xml";
	
	VTDGenHuge vg = new VTDGenHuge();
	System.out.println("Parsing");
	vg.parseFile(xml, true, VTDGenHuge.MEM_MAPPED);
	VTDNavHuge vn = vg.getNav();
	int schoolCount = 1;
	AutoPilotHuge aph = new AutoPilotHuge(vn);
	aph.selectXPath(xpath);
	while ((aph.evalXPath()) != -1) {
		System.out.println("School "+schoolCount++);
		
		int branchCount = 1;
		String childXpath = "./Location/Branch[Type = 'Residential']";
		
		vn.push();
		AutoPilotHuge aph1 = new AutoPilotHuge(vn);
		aph1.selectXPath(childXpath);
		while ((aph1.evalXPath()) != -1) {
			System.out.println("Branch "+branchCount++);
			String realType = getparsedValue(vn, "./RealType");
			Branch br = new Branch();
			br.setRealType(realType); 
			System.out.println(br);
		}
		vn.pop();
	}
}

Posted

in

,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *