在解析包含xi的xml时遇到问题:包含jaxb

我正在使用JAXB来解析xml。 我有一个如下的模式,以及在此模式上定义的两个xml文件a.xml和b.xml。 a.xml与b.xml通过xi:include xml标记有依赖关系。 请提交以下示例以获取更清晰的数据

I have followng schema definition:                  

这是两个xml文件:

A.XML:

   

B.XML:

    Name1   Name2  

现在我使用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); } } 

你应该补充一下

  spf.setNamespaceAware(true);