AnanThDev AnanThDev - 7 months ago 39
Java Question

How to get Attribute values from Select path or select Attribute ximpleware

<ConfiguredItems>
<OtapiConfiguredItem>
<Id>3117283038955</Id>
<Quantity>1693</Quantity>
<Configurators>
<ValuedConfigurator Pid="1627207" Vid="3232480" />
<ValuedConfigurator Pid="20509" Vid="28314" />
</Configurators>
</OtapiConfiguredItem>
<OtapiConfiguredItem>
<Id>3117283038956</Id>
<Quantity>1798</Quantity>
<Configurators>
<ValuedConfigurator Pid="1627207" Vid="3232480" />
<ValuedConfigurator Pid="20509" Vid="6145171" />
</Configurators>
</OtapiConfiguredItem>
<OtapiConfiguredItem>
<Id>3117283038957</Id>
<Quantity>1815</Quantity>
<Configurators>
<ValuedConfigurator Pid="1627207" Vid="28331" />
<ValuedConfigurator Pid="20509" Vid="28315" />
</Configurators>
</OtapiConfiguredItem>




The above is my XML .. I need to get ValuedConfigurator PID and VID attribute values for each OtapiConfiguredItem

I tried Select path vcPId.selectXPath("BatchItemFullInfoAnswer/Result/Item/ConfiguredItems/OtapiConfiguredItem/Configurators/ValuedConfigurator[@pid]");

Thanks in Advance..

Answer

There are many ways to do this in vtd-xml.. I give you a few options

First one does not use XPath to get to the attribute, but calls the cursor api's getAttrVal to do so...

import com.ximpleware.*;
public class queryAttr {
    public static void main(String[] s) throws VTDException{
        VTDGen vg = new VTDGen();
        vg.selectLcDepth(5);// improve XPath performance for deep document
        if (!vg.parseFile("input.xml", false))
            return;
        VTDNav vn = vg.getNav();
        AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath("/BatchItemFullInfoAnswer/Result/Item/ConfiguredItems/OtapiConfiguredItem/Configurators/ValuedConfigurator");
        int i=0,j=0;
        while((i=ap.evalXPath())!=-1){
            j= vn.getAttrVal("pid");
            if (j!=-1)
                System.out.println(" attr value for pid is ==>"+vn.toString(j));
            j= vn.getAttrVal("vid");
            if (j!=-1)
                System.out.println(" attr value for vid is ==>"+vn.toString(j));
        }
    }
}

Second one uses full XPath, read the comments embedded in teh code

import com.ximpleware.*;
public class queryAttr {
    public static void main(String[] s) throws VTDException{
        VTDGen vg = new VTDGen();
        vg.selectLcDepth(5);// improve XPath performance for deep document
        if (!vg.parseFile("input.xml", false))
            return;
        VTDNav vn = vg.getNav();
        AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath("/BatchItemFullInfoAnswer/Result/Item/ConfiguredItems/OtapiConfiguredItem/Configurators/ValuedConfigurator/@pid");
        AutoPilot ap2 = new AutoPilot(vn);
        ap2.selectXPath("../@vid");
        int i=0,j=0;
        while((i=ap.evalXPath())!=-1){
            System.out.println(" attr value for pid is ==>"+vn.toString(i+1)); // notice this is i+1, not i cuz i is the vtd record index for pid
            vn.push();// maintain consistency of autoPilot with push/pop combo
            if ((j=ap2.evalXPath())!=-1)
                System.out.println(" attr value for vid is ==>"+vn.toString(j+1)); // notice this is j+1, not j cuz j is the vtd record index for vid
            vn.pop();
        }
    }
}