我正在使用JAXB来解析xml.我有一个如下的模式,以及在此模式上定义的两个xml文件a.xml和b.xml. a.xml与b.xml通过xi:include xml标记有依赖关系.请提交以下示例以获取更清晰的数据 I have follown
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>
这是两个xml文件:
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>
现在我使用JAXB SAXFactory解析它:
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);
解析是成功的,但Details JAXB标记对象为null.无论如何,a.xml文件中的xi:include标记都没有展平.任何的想法?
试试这个,它肯定有效: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); } }