开发者

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

开发者 https://www.devze.com 2023-03-06 01:24 出处:网络
Given the following xml: <JUT> <DDT> <SSG q=\"textGoal\">Lorem ipsum...</SSG> </DDT>

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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消