0

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
            
        }
        
    }
technocrat
  • 701
  • 5
  • 13
  • 23
  • Do I need to create another AutoPilotHuge object and pass the childXpath query here. Any help appreciated. Thanks – technocrat Jul 27 '22 at 15:15
  • You have a typo in your XPath: `/*/School[starts-with(ScId,'RC')]/Location/Branch/[Type = 'Residential']` should be `/*/School[starts-with(ScId,'RC')]/Location/Branch[Type = 'Residential']` (the error is you have a `/` before the final predicate `[Type = 'Residential']`) – Conal Tuohy Jul 28 '22 at 00:57
  • @ConalTuohy Good catch there, but I need to know the next step of using this xpath to get the required value from each of the node which was filtered using the first xpath. – technocrat Jul 28 '22 at 03:08

1 Answers1

0

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();
        }
        
    }
technocrat
  • 701
  • 5
  • 13
  • 23