在解析包含xi的xml时遇到问题:包含jaxb [英] facing issue while parsing xml containing xi:includes with jaxb
问题描述
我正在使用JAXB来解析xml。我有一个如下的模式,以及在此模式上定义的两个xml文件a.xml和b.xml。 a.xml与b.xml通过xi:include xml标记有依赖关系。请提交以下示例以获取更清晰的数据
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>
以下是两个xml文件:
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>
现在我使用JAXB SAXFactory解析这个:
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);
解析成功,但 Details JAXB标记对象为空。无论如何,a.xml文件中的xi:include标记都没有被展平。任何想法?
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);
}
}
这篇关于在解析包含xi的xml时遇到问题:包含jaxb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!