编程语言
首页 > 编程语言> > java – VTD-XML大数据xPathExpression不使用名称空间

java – VTD-XML大数据xPathExpression不使用名称空间

作者:互联网

在运行命名空间时,我有一个小的测试文件.如果我删除命名空间这两项工作我是否在使用巨大的代码做错了什么?

更改为删除不可行的代码

    try {
        //without namespace works
        VTDGen vtdGen = new VTDGen();
        vtdGen.parseFile("test.xml", false);
        VTDNav vtdNav = vtdGen.getNav();
        AutoPilot autoPilot = new AutoPilot(vtdNav);
        autoPilot.selectXPath("//Receiver/Identifier");
        autoPilot.evalXPath();
        System.out.println("Stand===>" + vtdNav.getXPathStringVal() + " ===>");
    } catch(IndexOutOfBoundsException e) {
        e.printStackTrace();
    }

    try {
        //with namespace doesn't work
        VTDGen vtdGen = new VTDGen();
        vtdGen.parseFile("test.xml", true);
        VTDNav vtdNav = vtdGen.getNav();
        AutoPilot autoPilot = new AutoPilot(vtdNav);
        autoPilot.declareXPathNameSpace("x", "http://test/namespaces/ssfgf");
        autoPilot.selectXPath("//x:Receiver/Identifier");
        int index = autoPilot.evalXPath();
        System.out.println("Stand NS ===>" + vtdNav.toString(index) + " ===>");
    } catch(IndexOutOfBoundsException e) {
        e.printStackTrace();
    }

    try {
        //without namespace doesn't work
        VTDGenHuge vg = new VTDGenHuge();
        vg.parseFile("test.xml", false);
        VTDNavHuge vn = vg.getNav();
        AutoPilotHuge ap = new AutoPilotHuge(vn);
        ap.selectXPath("//Receiver/Identifier");
        ap.evalXPath();
        System.out.println("Huge ===> " + vn.toString(vn.getText()) + " ===>");
    } catch(IndexOutOfBoundsException e) {
        e.printStackTrace();
    }

    try {
        //with namespace doesn't work
        VTDGenHuge vg = new VTDGenHuge();
        vg.parseFile("test.xml", true);
        VTDNavHuge vn = vg.getNav();
        AutoPilotHuge ap = new AutoPilotHuge(vn);
        ap.declareXPathNameSpace("x", "http://test/namespaces/ssfgf");
        ap.selectXPath("//Receiver/Identifier");
        ap.evalXPath();
        System.out.println("Huge NS ===> " + vn.toString(vn.getText()) + " ===>");
    } catch(IndexOutOfBoundsException e) {
        e.printStackTrace();
    }

我得到了巨大的代码和NS标准的java.lang.IndexOutOfBoundsException

这是一个样本XML,遗憾的是无法显示真正的xml

<x:TestDocument xmlns:x="http://test/namespaces/ssfgf" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://test/namespaces/ssfgf">
    <x:TestHeader>
        <x:Type></x:Type>
        <x:Scopes>
            <x:Scope>
                <x:Identifier>Context</x:Identifier>
                <x:Type>CaseId</x:Type>
                <x:InstanceIdentifier>case1</x:InstanceIdentifier>
                <x:Business>
                    <x:Name>businessnane1</x:Name>
                </x:Business>
            </x:Scope>
            <x:Scope>
                <x:Identifier>Context</x:Identifier>
                <x:InstanceIdentifier>test1</x:InstanceIdentifier>
                <x:Type>TestId</x:Type>
            </x:Scope>
            <x:Scope>
                <x:Identifier>Context</x:Identifier>
                <x:InstanceIdentifier>other1</x:InstanceIdentifier>
                <x:Type>OtherId</x:Type>
            </x:Scope>
        </x:Scopes>
        <x:Receiver>
            <x:Identifier>testreceiverid</x:Identifier>
        </x:Receiver>
        <x:DocumentIdentification>
            <x:Type>type1</x:Type>
            <x:Identifier>id1</x:Identifier>
            <x:TypeVersion>version1</x:TypeVersion>
        </x:DocumentIdentification>
    </x:TestHeader>
    <x:TestBody attribute1="attribute1" attribute2="attribute2">
        <TestingData>testingdata1</TestingData>
    </x:TestBody>
</x:TestDocument>

解决方法:

由于一些问题,您将获得异常:一些使用您的代码,另一些使用扩展的VTD-xml.

首先,你没有使用VTD的第二个代码片段来启用命名空间感知.

您也没有设置名称空间绑定权限..

  ap.declareXPathNameSpace("x", "http://test/namespaces/ssfgf");

第三,getText()返回-1,这导致异常.

最后,// test将不匹配任何节点…所以getText()肯定会返回-1.

标签:java,vtd-xml
来源: https://codeday.me/bug/20190710/1426963.html