xml-parsing – 使用XInclude进行解析时“尝试解析XML文件时出错
我正在尝试使用XInclude创建一个组合的xml文档,以便通过JAXB进行解组.
这是我的解组代码: @Override public T readFromReader(final Reader reader) throws Exception { final Unmarshaller unmarshaller = createUnmarshaller(); final SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setXIncludeAware(true); spf.setNamespaceAware(true); //spf.setValidating(true); final XMLReader xr = spf.newSAXParser().getXMLReader(); final SAXSource source = new SAXSource( xr,new InputSource(reader) ); try { final T object = (T) unmarshaller.unmarshal(source); postReadSetup(object); return object; } catch (final Exception e) { throw new RuntimeException("Cannot parse XML: Additional information is attached. Please ensure your XML is valid.",e); } } 这是我的主要xml文件: <?xml version="1.0" encoding="UTF-8" ?> <tag1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xi="http://www.w3.org/2001/XInclude" xsi:schemaLocation="path-to-schema/schema.xsd"> <xi:include href="path-to-xml-files/included.xml"></xi:include> </tag1> 并包含了: <?xml version="1.0" encoding="UTF-8"?> <tag2> Some text </tag2> 为了实际解组它,我创建了一个新的FileReader,其中包含我的xml文件的路径(path-to-xml-files / main.xml – 路径是正确的,因为它可以清楚地找到主文件).但是,当我运行它时,包含的文件有问题.我收到带有链接SAXParseException的UnmarshalException,并显示以下错误消息:尝试解析XML文件时出错(href =’path-to-xml-files / included.xml’). 当我手动将included.xml的内容合并到main.xml中时,它运行没有问题. 我不知道它是JAXB问题还是XInclude问题,尽管我强烈怀疑后者. 我错过了什么? 解决方法
我用这个完全相同的问题打了三个小时,最后我发现了这个:
xerces.apache.org/xerces2-j/features.html 简而言之,您需要添加以下行: spf.setFeature(“http://apache.org/xml/features/xinclude/fixup-base-uris”,false); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |