1

我通过 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-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">

这是加载到 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/系统/附件-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.用原来的元素替换新元素。这似乎让我相信属性/ns 声明的顺序会在解析时影响 VTD。

注意:我使用两个 xml 文档(原始和后 dom4j xml)将 ns 感知的 xml 文档设置解析为“真”。此外,还会为每个 xml、original 和 post-dom4j 创建新的 VTD 对象。

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

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

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

4

1 回答 1

2

这似乎是 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

于 2012-05-11T00:22:20.693 回答