[英]facing issue while parsing xml containing xi:includes with jaxb
I am using JAXB to parse xml's. 我正在使用JAXB解析xml。 I have a schema as below and also two xml files a.xml and b.xml defined on this schema.
我有一个如下所示的架构,并且在此架构上定义了两个xml文件a.xml和b.xml。 a.xml have a dependency over b.xml thru xi:include xml tag.
a.xml通过xi:include xml标记对b.xml进行依赖。 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: 这是两个xml文件:
a.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: 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: 现在,我使用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);
The parsing is successfull but the Details JAXB tag object is null. 解析成功,但是Details JAXB标记对象为null。 Anyhow the xi:include tag in a.xml file is not flattened.
无论如何,a.xml文件中的xi:include标记不会变平。 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);
}
}
你应该添加
spf.setNamespaceAware(true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.