【问题标题】:Parsing xml with unbound prefixes解析带有未绑定前缀的 xml
【发布时间】:2011-01-20 05:52:43
【问题描述】:

尝试 SAX 解析

<foo bar:car="zoo"/>

结果:

org.dom4j.DocumentException: Error on line 1 of document file:///c:/temp/test.xml : The prefix "bar" for attribute "bar:car" associated with an element type "foo" is not bound. Nested exception: The prefix "bar" for attribute "bar:car" associated with an element type "foo" is not bound.
        at org.dom4j.io.SAXReader.read(SAXReader.java:482)
        at org.dom4j.io.SAXReader.read(SAXReader.java:321)
        at .<init>(<console>:18)
        at .<clinit>(<console>)
        at RequestResult$.<init>(<console>:9)
        at RequestResult$.<clinit>(<console>)
        at RequestResult$scala_repl_result(<console>)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.ja...

如何在不更改 XML 的情况下解决这个问题? (真正的 XML 来自我无法控制的 Web 服务)

【问题讨论】:

    标签: java xml namespaces sax


    【解决方案1】:

    禁用http://xml.org/sax/features/namespaces:

    xmlReader.setFeature("http://xml.org/sax/features/namespaces", false);
    

    这将阻止命名空间前缀转换为 URI。

    【讨论】:

    • 它不起作用。我试图从 org.dom4j.Document dom4jDoc = DocumentHelper.parseText(text); 复制代码并把 reader.setFeature("xml.org/sax/features/namespaces", false);但结果是一样的:org.xml.sax.SAXParseException: 元素“xx:service”的前缀“xx”未绑定。在 org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source) 我同意 xml 应该声明命名空间,但它不在我的控制范围内,而且很短。
    • @Uberto 并非所有 XMLReader 都支持将该功能设置为 false。您也可能在错误的上下文中设置它。要进行调试,请尝试在您调用之前查看该功能是否真的为真,并且在它之后实际上已设置为假。还要在读取出现不需要的命名空间错误的元素/属性之前检查它的值。
    猜你喜欢
    • 2015-01-27
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2013-01-30
    • 2015-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多