Faced with a problem while parsing xml containing xi: includes with jaxb

I am using JAXB to parse xml. I have a schema as shown below, as well as two xml files a.xml and b.xml defined in this schema. a.xml are dependent on b.xml via xi: include xml tag. Please provide an example below for clearer data.

 I have followng schema definition:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"  attributeFormDefault="unqualified">
    <xs:element name="Task">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="Details" minOccurs="1" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>
<xs:element name="Details">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="NAme" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>
</xs:schema>

Here are two xml files:

a.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Task xmlns:xi="http://www.w3.org/2001/XInclude">
<Details>
    <xi:include href="b.xml"/>
</Details>
</Task>

b.xml:

 <?xml version="1.0" encoding="UTF-8"?>
 <Detail>
  <Name>Name1</Name>
 </Detail>
<Detail>
   <Name>Name2</Name>
 </Detail>

Now I parse this using JAXB SAXFactory as:

 JAXBContext jaxbcon = JAXBContext.newInstance("schema-definition-jaxb-files");
 unmar = jaxbcon.createUnmarshaller();

 SAXParserFactory spf = SAXParserFactory.newInstance();
 spf.setXIncludeAware(true);
 XMLReader xr = spf.newSAXParser().getXMLReader();
 SAXSource source = new SAXSource(xr, new InputSource(new    FileInputStream(xmlfilename)));
 Object obj = unmar.unmarshal(source);

The parsing completed successfully, but the JAXB Details tag object is NULL. In any case, the xi: include tag in the a.xml file does not smooth out. any idea?

+3
source share
2 answers

Try it, it definitely works:

public class Test {
    public String include;

    public static void main(String[] args) throws Exception {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setXIncludeAware(true);
        spf.setNamespaceAware(true);
        XMLReader xr = spf.newSAXParser().getXMLReader();
        SAXSource src = new SAXSource(xr, new InputSource("test.xml"));
        Test t = JAXB.unmarshal(src, Test.class);
        System.out.println(t.include);
    }
}
+3
source

You added

    spf.setNamespaceAware(true);
+1
source

All Articles