SAXParser不允许使用Xinclude

时间:2014-11-04 10:11:29

标签: java xml sax xinclude

我正在尝试解组其中包含<xi:include>标记的xml文档。但SAXParser不允许这样做,即使我特别告诉SAXParserFactory允许它。

Java代码:

SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setXIncludeaware(true);
spf.setNamespaceAwere(true);

spf.setFeature("http://apache.org/xml/features/xinclude", true);
spf.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", true);

XMLReader xr = spf.newSAXParser().getXMLReader();
SAXSource source = new SAXSource(xr, new InputSource(inputStream));
JAXBElement<MyClass> el = unmarshaller.unmarshal(source, MyClass.class);

要阅读的XML文档

<?xml version="1.0" encoding="UTF-8"?>
<extension xmlns="http://www.example.com/test" xmlns:ext="http://www.example.com/test" xmlns:xi="http://www.w3.org/2003/XInclude">
    <visibility>
        <resourceConstraints>
            <resourceConstraint ext:resourceId="resourceOne">
                <role ext:show="true">AdminUsers</role>
            </resourceConstraint>
            <resourceConstraint ext:resourceId="resourceTwo">
                <role ext:show="true">AdminUsers</role>
            </resourceConstraint>
        </resourceConstraints>
        <xi:include href="extraContent.xml" />
    </visibility>
</extension>

当我运行它时,我得到了这个例外:

org.xml.sax.SAXParseException; lineNumber: 12; columnNumber: 50; cvc-complex-type.2.4.a: Invalid content was found starting with element 'xi:include'. One of '{"http://www.example.com/test":resourceConstraints}' is expected.

当我从XML文档中删除<xi:include>标记时,该文件被解组很好。 unmarshaller附加了一个模式。架构不允许<xi:include>

1 个答案:

答案 0 :(得分:1)

我使用了xmlns:xi="http://www.w3.org/2003/XInclude",而我应该使用xmlns:xi="http://www.w3.org/2001/XInclude"

问题现已解决!