Java VTDGen VTDGenHuge, get details after positionning with XPath

66 Views Asked by At

I have the following xml file :

<?xml version="1.0" encoding="UTF-8"?>
<ns:t xmlns:ns="http://www.myaddress.com">
    <ns:cs>
        <ns:c name="A">
            <ns:d key="ka1">
                <ns:v>value for ka1</ns:v>
            </ns:d>
            <ns:d key="ka2">
                <ns:v>my value</ns:v>
            </ns:d>
            <ns:d key="k a 3">
                <ns:v>k a 3 : what an example</ns:v>
            </ns:d>
        </ns:c>
        <ns:c name="B">
            <ns:d key="b key">
                <ns:v>my value for b key</ns:v>
            </ns:d>
        </ns:c>
        <ns:c name="C">
            <ns:d key="c key">
                <ns:v>value 1</ns:v>
            </ns:d>
            <ns:d key="c key 2">
                <ns:v>value</ns:v>
            </ns:d>
        </ns:c>
    </ns:cs>
    <ns:rs>
        <ns:r>
            <ns:d key="D">
                <ns:v>d value</ns:v>
            </ns:d>
            <ns:d key="E">
                <ns:v>E VALUE</ns:v>
            </ns:d>
            <ns:d key="F">
                <ns:v>F</ns:v>
            </ns:d>
            <ns:d key="G">
                <ns:v>G</ns:v>
            </ns:d>
            <ns:d key="H">
                <ns:v>my H</ns:v>
            </ns:d>
        </ns:r>
    </ns:rs>
</ns:t>

java code snippet: (Just the code to isolate the issue)

String inputFile = "myFile.xml";
VTDGenHuge vgh = new VTDGenHuge();
if (vgh.parseFile(inputFile,true,VTDGenHuge.MEM_MAPPED)){
    VTDNavHuge vnh = vgh.getNav();
    AutoPilotHuge aph = new AutoPilotHuge(vnh);
    aph.declareXPathNameSpace("dat","http://www.myaddress.com");
    aph.selectXPath("//dat:cs/dat:c");

    while (aph.evalXPath()!=-1){
        String name = vnh.toRawString(vnh.getAttrVal("name"));
        LOGGER.debug(name);

        // here I want to iterate over dat:d and get dat:v
        while ( ???? ){
            String key =  ??? something like : vnh.toRawString(vnh.getAttrVal("key"));
            LOGGER.debug(key);
            String value =  ??? 
            LOGGER.debug(value);
        }
    }
}

Here is the output (without while ????) :

A
B
C

But for each c part, I want the detail (ns:d key attribute, and ns:v value)

Output I want :

A
ka1
value for ka1
ka2
my value
k a 3
k a 3 : what an example
B
b key
my value for b key
C
c key
value 1
c key 2
value

I know there is something I don't understand with VTDGen's logic...

I succeded with this code :

aph.selectXPath("//dat:cs/dat:c");
while (aph.evalXPath()!=-1){
    String name = vnh.toRawString(vnh.getAttrVal("name"));
    LOGGER.debug(name);
    aph2.selectXPath("//dat:cs/dat:c[@name=\"" + name + "\"]/dat:d");
    while (aph2.evalXPath()!=-1){
        String key = vnh.toRawString(vnh.getAttrVal("key"));
        LOGGER.debug(key);
        aph3.selectXPath("//dat:cs/dat:c[@name=\"" + name + "\"]/dat:d[@key=\"" + key + "\"]/dat:v");
        while (aph3.evalXPath()!=-1){
            String value = vnh.toRawString(vnh.getText());
            LOGGER.debug(value);
        }
    }
}

Is there a more efficient way ?

0

There are 0 best solutions below