Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting text of attribute in xml using vtd-xml in java

Given the following xml:

<JUT>
    <DDT>
        <SSG q="textGoal">Lorem ipsum...</SSG>
    </DDT>
    ....
    ...
</JUT>

I am using vtd-xml with XPath in order to retrieve 'textGoal' as follows:

        VTDGen vg = new VTDGen();
        vg.setDoc(xmlContent);
        vg.parse(false);
        VTDNav vn = vg.getNav();
        AutoPilot ap = new AutoPilot(vn);
        int node = 0;

        ap.selectXPath("//SSG[1]/@q");
        node = ap.evalXPath();
        if(node != -1) {
            myString = vn.toString(node);
        }

This gives myString as 'q' and not 'textGoal'. I have two questions:

  1. What am I doing wrong?
  2. I know that 'textGoal' is URL-escaped. Does vtd-xml do URL-UNescape or do I have to do this myself?

Regards

like image 905
Alex Avatar asked Feb 24 '23 05:02

Alex


2 Answers

Use vn.getAttributeVal(vn.toString(node))

like image 115
Kal Avatar answered Feb 26 '23 21:02

Kal


Another way of doing it is

vn.toString(node+1) 

assuming node is not -1. As to the URL escaping, part, you have toString(), toRawString(), and toNormalizedString() to choose from

like image 26
vtd-xml-author Avatar answered Feb 26 '23 20:02

vtd-xml-author