4

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

Alex
  • 616
  • 1
  • 12
  • 28

2 Answers2

5

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

Kal
  • 24,724
  • 7
  • 65
  • 65
3

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

vtd-xml-author
  • 3,319
  • 4
  • 22
  • 30
  • 1
    But what to do when my path is variable? How should I know if it requests an attribute (call `getAttributeVal(vn.toString(node))` or `toString(node+1)`) or not (call `toString(node)`)? – halloei Jan 15 '15 at 14:07
  • What do you mean by variable path? – vtd-xml-author Jan 15 '15 at 18:33
  • 1
    The user can put in the XPath `/foo/bar/text()`, `/foo/bar/@attr` or `/foo[@attr='baz']/bar/text()` - I don't know. How do I know which of the vtd-xml methods to use? Or why isn't there an all-in-one method? – halloei Jan 16 '15 at 07:57
  • because there are different user requirements... this has to do with the xml spec of the treatment of string.... In general, toString() is the most common – vtd-xml-author Jan 19 '15 at 19:54
  • @Alex did you ever get a generic solution in the case where your xpath is a variable, and you wouldn't know a priori if it's an attribute or not? – Roy Truelove Sep 21 '15 at 19:28
  • 2
    I think that xpath evaluation returning attribute name node index is consistent with thw design goal of vtd-xml. One general solution would be to use autopilot's evalPathToString method which directly gives you attr val string. – vtd-xml-author Sep 22 '15 at 22:42
  • Thanks, evalXPathToString works although it's not perfect. Do you think it's possible to add a .isAttribute method to the Navigator? There is already an .isElement() – George Sofianos Apr 11 '18 at 11:07
  • @GeorgeSofianos --sorry I do not understand... can you provide an example of your suggested isAttribute? I don't know isElement() is there ... – vtd-xml-author Apr 12 '18 at 20:47
  • Actually you are right, it's not actually "there". It's a protected method in VTDNav.java - I think I only used it in my debugger but I remember it returns false for an attribute node - I suppose this can't be used to check if the XPath result is an element or an attribute? – George Sofianos Apr 13 '18 at 11:15