使用xpath和vtd-xml将元素的子节点和文本作为字符串 [英] Using xpath and vtd-xml to get sub nodes and text of an element as a string

查看:140
本文介绍了使用xpath和vtd-xml将元素的子节点和文本作为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的XML的一部分:

This is a portion of my 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但是没有用。

In vtd-xml I used an AutoPilot with XPath "//L/D" and "//L/D/text()" but that did not work.

任何建议或其他方法将不胜感激。

Any advice or alternative approach will be appreciated.

问候

推荐答案

以下是做你想要的代码。

Below is the code that does what you are looking for.

    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)));
        }
    }

这篇关于使用xpath和vtd-xml将元素的子节点和文本作为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆