在java中使用vtd-xml获取xml中的属性文本

时间:2023-02-07 15:09:00

Given the following xml:

给出以下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:

我正在使用带有XPath的vtd-xml来检索'textGoal',如下所示:

        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:

这使myString为'q'而不是'textGoal'。我有两个问题:

  1. What am I doing wrong?
  2. 我究竟做错了什么?

  3. I know that 'textGoal' is URL-escaped. Does vtd-xml do URL-UNescape or do I have to do this myself?
  4. 我知道'textGoal'是网址转义的。 vtd-xml是做URL-UNescape还是我自己必须这样做?

Regards

2 个解决方案

#1


5  

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

#2


2  

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

假设节点不是-1。至于URL转义,部分,你有toString(),toRawString()和toNormalizedString()可供选择

#1


5  

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

#2


2  

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

假设节点不是-1。至于URL转义,部分,你有toString(),toRawString()和toNormalizedString()可供选择