6
votes

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?

2
What does your domain model look like? - bdoughan
Blaise, sorry I didnt get you. waht is domain model? - Darshan Nair
Yes, I'm curious what you are trying to map to. - bdoughan
Hi Blaise thanks.... since the space provided here is less, I have create a new stackoverflow question with title "facing issue while parsing xml containing xi:includes with jaxb". Can you please look at this and help me how to resolve this issue. Thanks, Darshan - Darshan Nair
Schema typo? <xs:element name="NAme" -> "Name"? And in your schema you've defined Details as a sequence of Name elements, while you're really getting a sequence of Detail elements inside your Details element... - Torious

2 Answers

4
votes

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);
    }
}
2
votes

You shoud add

    spf.setNamespaceAware(true);