I am using JAXB to parse xml's. I have a schema as below and also two xml files a.xml and b.xml defined on this schema. a.xml have a dependency over b.xml thru xi:include xml tag. Please file the below example for more clear 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 the 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 am parsing 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 is successfull but the Details JAXB tag object is null. Anyhow the xi:include tag in a.xml file is not flattened. any idea?
Try this, 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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With