javaxpathvtd-xml

VTD xml select child nodes


I have the school data in xml file as structured below.

<?xml version="1.0" encoding="ISO-8859-1"?>
<SchoolData>
<School>
<ScId>SC101-91</ScId>
    <Location>
      <Branch>
        <BranchId>Br-111</BranchId>
        <Type>Residential</Type>
        <RealType>Residential</RealType>
      </Branch>
      <Branch>
        <BranchId>BR-222</BranchId>
        <Type>Daycare</Type>
        <RealType>Daycare</RealType>
      </Branch>
      <Branch>
        <BranchId>Br-333</BranchId>
        <Type>Unknown</Type>
        <RealType>Unknown</RealType>
      </Branch>
    </Location>
</School>

<School>
<ScId>RC101-92</ScId>
    <Location>
      <Branch>
        <BranchId>Br-111</BranchId>
        <Type>Residential</Type>
        <RealType>Residential</RealType>
      </Branch>
      <Branch>
        <BranchId>BR-222</BranchId>
        <Type>Daycare</Type>
        <RealType>Daycare</RealType>
      </Branch>
      <Branch>
        <BranchId>Br-333</BranchId>
        <Type>Unknown</Type>
        <RealType>Unknown</RealType>
      </Branch>
    </Location>
</School>
</SchoolData>

I am filtering all the school nodes based on a condition using xpath expression as /*/School[starts-with(ScId,'RC')]

While I am iterating over each school node, I need to create branch object based on the type.

I have made the xpath expression for the same but not sure how to implement using VTD.

I have following parser code and unable to select the branch node and create respective branch object.

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";
        
        final VTDGenHuge vg = new VTDGenHuge();
        System.out.println("Parsing");
        vg.parseFile(xml, true, VTDGenHuge.MEM_MAPPED);

        VTDNavHuge vn = vg.getNav();

        AutoPilotHuge aph = new AutoPilotHuge(vn);
        aph.selectXPath(xpath);
        while ((aph.evalXPath()) != -1) {
            String childXpath = "/*/School[starts-with(ScId,'RC')]/Location/Branch/[Type = 'Residential']";
            Branch br1 = new Branch();
            br1.setRealType(""); // get the value from the 'Branch' child node of this school node
            
        }
        
    }

Solution

  • I have figured it out how to reach the child node and then get back to parent while loop using VTDNavHuge push() and VTDNavHuge pop() methods.

    I'm adding the implementation code here for your help.

    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";
            
            final 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();
            }
            
        }