3
我想解組一個內部有<xi:include>
標記的xml文檔。但即使我具體告訴SAXParserFactory允許它,SAXParser也不允許這樣做。SAXParser不允許Xinclude
的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.
當我從刪除<xi:include>
標籤XML文檔,文件解組就好了。解組器附有一個模式。模式不允許<xi:include>
。