我正在尝试解组一个包含< xi:include>的xml文档.里面的标签.但SAXParser不允许这样做,即使我特别告诉SAXParserFactory允许它.
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.
当我删除< xi:include>从XML文档中标记,文件解组就好了. unmarshaller附加了一个模式.架构不允许< xi:include>.
解决方法:
我使用xmlns:xi =“http://www.w3.org/2003/XInclude”,而我应该使用xmlns:xi =“http://www.w3.org/2001/XInclude”
问题现在解决了!