VTD-XML异常:名称空间限定异常:带前缀的属性不限定 [英] VTD-XML Exception: Name space qualification Exception: prefixed attribute not qualified

查看:106
本文介绍了VTD-XML异常:名称空间限定异常:带前缀的属性不限定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过Web服务接收XML,并且我正在使用旧版代码(使用dom4j)执行一些xml转换.将原始XML加载/解析为VTD-XML(VTDGen)可以正常工作,不会引发任何异常.但是,在将xml加载到dom4j中之后,我注意到一些元素名称空间声明和属性已重新排列.显然,这种重新安排导致VTD-XML引发以下异常:

例外: 名称空间限定异常:带前缀的属性不限定

行号:101偏移:1827

以下是原始XML中此行号处的元素:

< RR_PerformanceSite:PerformanceSite_1_4 RR_PerformanceSite:FormVersion ="1.4" xmlns:NSF_ApplicationChecklist ="http://apply.grants.gov/forms/NSF_ApplicationChecklist-V1.1" xmlns:NSF_CoverPage ="http://apply.grants .gov/forms/NSF_CoverPage-V1.1"xmlns:NSF_DeviationAuthorization =" http://apply.grants.gov/forms/NSF_DeviationAuthorization-V1.1"xmlns:NSF_Registration =" http://apply.grants.gov/forms/NSF_Registration-V1.1"xmlns:NSF_SuggestedReviewers =" http://apply.grants.gov/forms/NSF_SuggestedReviewers-V1.1"xmlns:PHS398_CareerDevelopmentAwardSup =" http://apply.grants.gov/forms/PHS398_CareerDevelopmentAwardSup_1 .1"xmlns:PHS398_Checklist =" http://apply.grants.gov/forms/PHS398_Checklist_1_3-V1.3"xmlns:PHS398_CoverPageSupplement =" http://apply.grants.gov/forms/PHS398_CoverPageSupplement_1_4-V1.4"xmlns :PHS398_ModularBudget ="http://apply.grants.gov/forms/PHS398_ModularBudget-V1.1" xmlns:PHS398_ResearchPlan ="http://apply.grants.gov/forms/PHS398_ResearchPlan_1_3-V1.3" xmlns:PHS_CoverLetter ="http://apply.grants.gov/forms/PHS_CoverLetter_1_2-V1.2"xmlns:RR_Budget =" http://apply.grants.gov/forms/RR_Budget-V1.1"xmlns:RR_KeyPersonExpanded =" http://apply.grants.gov/forms/RR_KeyPersonExpanded_1_2-V1.2"xmlns:RR_OtherProjectInfo =" http://apply.grants.gov/forms/RR_OtherProjectInfo_1_2-V1.2"xmlns:RR_PerformanceSite =" http://apply.grants. gov/forms/PerformanceSite_1_4-V1.4"xmlns:RR_PersonalData =" http://apply.grants.gov/forms/RR_PersonalData-V1.1"xmlns:RR_SF424 =" http://apply.grants.gov/forms/RR_SF424_1_2-V1.2"xmlns:RR_SubawardBudget =" http://apply.grants.gov/forms/RR_SubawardBudget-V1.2"xmlns:SF424C =" http://apply.grants.gov/forms/SF424C-V1. 0"xmlns:att =" http://apply.grants.gov/system/Attachments-V1.0"xmlns:codes =" http://apply.grants.gov/system/UniversalCodes-V2.0"xmlns: globlib ="http://apply.grants.gov/system/GlobalLibrary-V2.0">

以下是加载到dom4j中的相同元素:

< RR_PerformanceSite:PerformanceSite_1_4 xmlns:RR_PerformanceSite ="http://apply.grants.gov/forms/PerformanceSite_1_4-V1.4" xmlns:NSF_ApplicationChecklist ="http://apply.grants.gov/forms/NSF_ApplicationChecklist- V1.1"xmlns:NSF_CoverPage =" http://apply.grants.gov/forms/NSF_CoverPage-V1.1"xmlns:NSF_DeviationAuthorization =" http://apply.grants.gov/forms/NSF_DeviationAuthorization-V1.1"xmlns:NSF_Registration ="http://apply.grants.gov/forms/NSF_Registration-V1.1" xmlns:NSF_SuggestedReviewers ="http://apply.grants.gov/forms/NSF_SuggestedReviewers-V1.1" xmlns:PHS398_CareerDevelopmentAwardSup = "http://apply.grants.gov/forms/PHS398_CareerDevelopmentAwardSup_1_1-V1.1" xmlns:PHS398_Checklist ="http://apply.grants.gov/forms/PHS398_Checklist_1_3-V1.3" xmlns:PHS398_CoverPageSupplement ="http://apply.grants.gov/forms/PHS398_CoverPageSupplement_1_4-V1.4"xmlns:PHS398_ModularBudget =" http://apply.grants.gov/forms/PHS398_ModularBudget-V1.1"xmlns:PHS398_ResearchPlan =" http://apply.grants .gov/forms/PHS398_R esearchPlan_1_3-V1.3"xmlns:PHS_CoverLetter =" http://apply.grants.gov/forms/PHS_CoverLetter_1_2-V1.2"xmlns:RR_Budget =" http://apply.grants.gov/forms/RR_Budget-V1. 1"xmlns:RR_KeyPersonExpanded =" http://apply.grants.gov/forms/RR_KeyPersonExpanded_1_2-V1.2"xmlns:RR_OtherProjectInfo =" http://apply.grants.gov/forms/RR_OtherProjectInfo_1_2-V1.2"xmlns: RR_PersonalData ="http://apply.grants.gov/forms/RR_PersonalData-V1.1" xmlns:RR_SF424 ="http://apply.grants.gov/forms/RR_SF424_1_2-V1.2" xmlns:RR_SubawardBudget ="http ://apply.grants.gov/forms/RR_SubawardBudget-V1.2"xmlns:SF424C =" http://apply.grants.gov/forms/SF424C-V1.0"xmlns:att =" http://apply .grants.gov/system/Attachments-V1.0"xmlns:codes =" http://apply.grants.gov/system/UniversalCodes-V2.0"xmlns:globlib =" http://apply.grants.gov/system/GlobalLibrary-V2.0"RR_PerformanceSite:FormVersion =" 1.4>

问题在于新XML元素中的属性(在元素末尾的偏移量1827):RR_PerformanceSite:FormVersion ="1.4"

这是删除异常的原因: 1.将此元素的RR_PerformanceSite xmlns声明添加到XML文档的根元素中. 2.用原始元素替换新元素.这个SEEMS使我相信解析时,属性/ns声明的顺序会影响VTD.

注意:我将两个xml文档(原始和dom4j以后的xml)解析为ns的xml doc ns.此外,还会为每个xml,原始dom4j和post-dom4j创建新的VTD对象.

我尝试将'RR_PerformanceSite:FormVersion ="1.4"'像原始元素一样放在元素的开头,但这并没有消除异常.由于属性位置的更改,错误消息中的偏移量有所不同. xmlns声明的顺序会影响VTD吗?

我查看了VTDGen源代码,无法弄清为什么会引发此异常.

为什么dom4j解析新文档而vtd无法解析?任何人都可以对此有所了解吗?

解决方案

这似乎是VTD-XML上的一个错误,与命名空间声明顺序有关.

始终可以使用以下Java代码复制

public class SchemaTester {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {

        String bad = "C:/Temp/VTD_bad.xml"; // XML files to test
        String good = "C:/Temp/VTD_good.xml";

        StringBuilder sb = new StringBuilder();

        char[] buf = new char[4*1024];
        FileReader fr = new FileReader(bad);
        int readed = 0;

        while ((readed = fr.read(buf, 0, buf.length)) != -1) {
            sb.append(buf, 0, readed);
        }

        fr.close();

        String x = sb.toString();

        //instantiate VTDGen
        //and call parse 
        VTDGen vg = new VTDGen();
        vg.setDoc(x.getBytes("UTF-8"));
        vg.parse(true);  // set namespace awareness to true
        VTDNav vn = vg.getNav();



        AutoPilot ap = new AutoPilot (vn);
        ap.selectXPath("//*/@*");

        int i= -1;
        while((i=ap.evalXPath()) != -1) {
            // i will be attr name, i+1 will be attribute value
            System.out.println("\t\tAttribute ==> " + vn.toNormalizedString(i));
            System.out.println("\t\tValue ==> " + vn.toNormalizedString(i+1));
        } 

    }
}

OP已将XML上传到 https://gist.github.com/2696220

I receive an XML via a web service and I am using legacy code (which uses dom4j) to perform some xml transformation. Loading/parsing the original XML into VTD-XML (VTDGen) works fine, no exceptions thrown. However, after loading the xml into dom4j, I noticed some of the element namespace declarations and attributes are re-arranged. Apparently, this re-arrangement causes VTD-XML to throw the following exception:

Exception: Name space qualification Exception: prefixed attribute not qualified

Line Number: 101 Offset: 1827

Here is the element at this line number in the original XML:

<RR_PerformanceSite:PerformanceSite_1_4 RR_PerformanceSite:FormVersion="1.4" xmlns:NSF_ApplicationChecklist="http://apply.grants.gov/forms/NSF_ApplicationChecklist-V1.1" xmlns:NSF_CoverPage="http://apply.grants.gov/forms/NSF_CoverPage-V1.1" xmlns:NSF_DeviationAuthorization="http://apply.grants.gov/forms/NSF_DeviationAuthorization-V1.1" xmlns:NSF_Registration="http://apply.grants.gov/forms/NSF_Registration-V1.1" xmlns:NSF_SuggestedReviewers="http://apply.grants.gov/forms/NSF_SuggestedReviewers-V1.1" xmlns:PHS398_CareerDevelopmentAwardSup="http://apply.grants.gov/forms/PHS398_CareerDevelopmentAwardSup_1_1-V1.1" xmlns:PHS398_Checklist="http://apply.grants.gov/forms/PHS398_Checklist_1_3-V1.3" xmlns:PHS398_CoverPageSupplement="http://apply.grants.gov/forms/PHS398_CoverPageSupplement_1_4-V1.4" xmlns:PHS398_ModularBudget="http://apply.grants.gov/forms/PHS398_ModularBudget-V1.1" xmlns:PHS398_ResearchPlan="http://apply.grants.gov/forms/PHS398_ResearchPlan_1_3-V1.3" xmlns:PHS_CoverLetter="http://apply.grants.gov/forms/PHS_CoverLetter_1_2-V1.2" xmlns:RR_Budget="http://apply.grants.gov/forms/RR_Budget-V1.1" xmlns:RR_KeyPersonExpanded="http://apply.grants.gov/forms/RR_KeyPersonExpanded_1_2-V1.2" xmlns:RR_OtherProjectInfo="http://apply.grants.gov/forms/RR_OtherProjectInfo_1_2-V1.2" xmlns:RR_PerformanceSite="http://apply.grants.gov/forms/PerformanceSite_1_4-V1.4" xmlns:RR_PersonalData="http://apply.grants.gov/forms/RR_PersonalData-V1.1" xmlns:RR_SF424="http://apply.grants.gov/forms/RR_SF424_1_2-V1.2" xmlns:RR_SubawardBudget="http://apply.grants.gov/forms/RR_SubawardBudget-V1.2" xmlns:SF424C="http://apply.grants.gov/forms/SF424C-V1.0" xmlns:att="http://apply.grants.gov/system/Attachments-V1.0" xmlns:codes="http://apply.grants.gov/system/UniversalCodes-V2.0" xmlns:globlib="http://apply.grants.gov/system/GlobalLibrary-V2.0">

Here is the same element after loaded into dom4j:

<RR_PerformanceSite:PerformanceSite_1_4 xmlns:RR_PerformanceSite="http://apply.grants.gov/forms/PerformanceSite_1_4-V1.4" xmlns:NSF_ApplicationChecklist="http://apply.grants.gov/forms/NSF_ApplicationChecklist-V1.1" xmlns:NSF_CoverPage="http://apply.grants.gov/forms/NSF_CoverPage-V1.1" xmlns:NSF_DeviationAuthorization="http://apply.grants.gov/forms/NSF_DeviationAuthorization-V1.1" xmlns:NSF_Registration="http://apply.grants.gov/forms/NSF_Registration-V1.1" xmlns:NSF_SuggestedReviewers="http://apply.grants.gov/forms/NSF_SuggestedReviewers-V1.1" xmlns:PHS398_CareerDevelopmentAwardSup="http://apply.grants.gov/forms/PHS398_CareerDevelopmentAwardSup_1_1-V1.1" xmlns:PHS398_Checklist="http://apply.grants.gov/forms/PHS398_Checklist_1_3-V1.3" xmlns:PHS398_CoverPageSupplement="http://apply.grants.gov/forms/PHS398_CoverPageSupplement_1_4-V1.4" xmlns:PHS398_ModularBudget="http://apply.grants.gov/forms/PHS398_ModularBudget-V1.1" xmlns:PHS398_ResearchPlan="http://apply.grants.gov/forms/PHS398_ResearchPlan_1_3-V1.3" xmlns:PHS_CoverLetter="http://apply.grants.gov/forms/PHS_CoverLetter_1_2-V1.2" xmlns:RR_Budget="http://apply.grants.gov/forms/RR_Budget-V1.1" xmlns:RR_KeyPersonExpanded="http://apply.grants.gov/forms/RR_KeyPersonExpanded_1_2-V1.2" xmlns:RR_OtherProjectInfo="http://apply.grants.gov/forms/RR_OtherProjectInfo_1_2-V1.2" xmlns:RR_PersonalData="http://apply.grants.gov/forms/RR_PersonalData-V1.1" xmlns:RR_SF424="http://apply.grants.gov/forms/RR_SF424_1_2-V1.2" xmlns:RR_SubawardBudget="http://apply.grants.gov/forms/RR_SubawardBudget-V1.2" xmlns:SF424C="http://apply.grants.gov/forms/SF424C-V1.0" xmlns:att="http://apply.grants.gov/system/Attachments-V1.0" xmlns:codes="http://apply.grants.gov/system/UniversalCodes-V2.0" xmlns:globlib="http://apply.grants.gov/system/GlobalLibrary-V2.0" RR_PerformanceSite:FormVersion="1.4">

The problem is regarding the attribute (at offset 1827, at the end of the element) in the new XML element: RR_PerformanceSite:FormVersion="1.4"

Here is what removes the exception: 1. Adding the RR_PerformanceSite xmlns declaration for this element to the root element of the XML doc. 2. Replacing new element with original element. This SEEMS to lead me to believe that the order of the attributes/ns declarations affects VTD when parsing.

NOTE: I parse the xml doc setting ns aware to 'true' with both xml docs (original and post-dom4j xml). Also, new VTD objects are created for each xml, original and post-dom4j.

I tried to put 'RR_PerformanceSite:FormVersion="1.4"' at the beginning of the element like the original but that does not remove the exception. The offset in the error message is different due to the change of location of the attribute. Does the order of the xmlns declarations affect VTD?

I have looked at the VTDGen source code and cannot figure out why this exception is being thrown.

Why would dom4j parse the new doc and vtd is unable to? Can anyone can shed some light on this?

解决方案

It appears to be a bug on VTD-XML, related with namespace declaration order.

Always reproducible using the following Java code

public class SchemaTester {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {

        String bad = "C:/Temp/VTD_bad.xml"; // XML files to test
        String good = "C:/Temp/VTD_good.xml";

        StringBuilder sb = new StringBuilder();

        char[] buf = new char[4*1024];
        FileReader fr = new FileReader(bad);
        int readed = 0;

        while ((readed = fr.read(buf, 0, buf.length)) != -1) {
            sb.append(buf, 0, readed);
        }

        fr.close();

        String x = sb.toString();

        //instantiate VTDGen
        //and call parse 
        VTDGen vg = new VTDGen();
        vg.setDoc(x.getBytes("UTF-8"));
        vg.parse(true);  // set namespace awareness to true
        VTDNav vn = vg.getNav();



        AutoPilot ap = new AutoPilot (vn);
        ap.selectXPath("//*/@*");

        int i= -1;
        while((i=ap.evalXPath()) != -1) {
            // i will be attr name, i+1 will be attribute value
            System.out.println("\t\tAttribute ==> " + vn.toNormalizedString(i));
            System.out.println("\t\tValue ==> " + vn.toNormalizedString(i+1));
        } 

    }
}

The OP has uploaded the XML to https://gist.github.com/2696220

这篇关于VTD-XML异常:名称空间限定异常:带前缀的属性不限定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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