如何使用java

时间:2016-10-10 08:45:55

标签: java xml vtd-xml

XML:

  <Document xmlns="urn:abc:xyz">
<A>
    <B>
    </B>
</A>

需要获取xmlns的值ieurn:abc:xyz。我是vtd-xml的新手,刚试过一些帖子。以下是我到目前为止所做的事情。

 String testData = context.getXmlFragment().toString().trim();
        final VTDGen vg = new VTDGen();
        vg.setDoc(testData.getBytes());

        try {
            vg.parse(true);
            VTDNav vn = vg.getNav();
            final AutoPilot apParty = new AutoPilot(vn);
            apParty.selectXPath("/Document");

            int i = -1;
            while ((i = apParty.evalXPath()) != -1) {
                long l = vn.getContentFragment();
                System.out.println(" -==> " + vn.toString((int) l, (int) (l >> 32)));
            }
        }catch (XPathEvalException e) {
            e.printStackTrace();
        } 

1 个答案:

答案 0 :(得分:1)

以下是在XPath中进行命名空间节点查询的代码...

import com.ximpleware.*;
public class queryNS {
    public static void main(String[] args) throws VTDException {
        // TODO Auto-generated method stub
        VTDGen vg = new VTDGen();
        String xml = "<test xmlns='xyz'><a/></test>";
        byte[] ba = xml.getBytes();
        vg.setDoc(ba);
        vg.parse(true);
        VTDNav vn = vg.getNav();
        AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath("/test/namespace::node()");
        int i=0;
        while((i=ap.evalXPath())!=-1){
            System.out.println(" value ===>"+vn.toString(i+1));
        }
    }

}