这是我的 XML 的一部分:
<MAIN>
<L>
<D>string1 string2 <b>string3</b> string4</D>
</L>
<L>
<D>string5 string6 <b>string7</b> string8 <i>string9</i></D>
</L>
</MAIN>
I want to get the content of all the <D> tags as string. So, the example above should return:
1st iteration: 'string1 string2 <b>string3</b> string4'
2nd iteration: 'string5 string6 <b>string7</b> string8 <i>string9</i>'
etc...
在 vtd-xml 中,我使用了带有 XPath“//L/D”和“//L/D/text()”的 AutoPilot,但这没有用。
任何建议或替代方法将不胜感激。
问候
最佳答案
下面是执行您正在寻找的代码。
VTDGen vg = new VTDGen();
if (vg.parseFile("c://xml//alex.txt", true)){
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("//L/D");
int i=-1;
while((i=ap.evalXPath())!=-1){
long l = vn.getContentFragment();
System.out.println(" -==> "+ vn.toString((int )l, (int)(l>>32)));
}
}
关于java - 使用xpath和vtd-xml获取元素的子节点和文本作为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6004322/