Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SAXParser doesn't allow Xinclude

I'm trying to unmarshal an xml document that has the <xi:include> tag inside it. But SAXParser doens't allow this even though I specificly tell the SAXParserFactory to allow it.

The Java code:

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 Document to read

<?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>

When I run it I get this exception:

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.

When I remove the <xi:include> tag from the XML document, the file is unmarshalled just fine. The unmarshaller has a schema attached to it. The schema doens't allow the <xi:include>.

like image 335
Erates Avatar asked Nov 10 '22 23:11

Erates


1 Answers

I used xmlns:xi="http://www.w3.org/2003/XInclude" while I should have used xmlns:xi="http://www.w3.org/2001/XInclude"

Problem is fixed now!

like image 199
Erates Avatar answered Nov 14 '22 21:11

Erates