cha*_*ama 5 xinclude jaxb xml-parsing
我正在尝试使用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);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的主要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>
Run Code Online (Sandbox Code Playgroud)
并包含了:
<?xml version="1.0" encoding="UTF-8"?>
<tag2> Some text </tag2>
Run Code Online (Sandbox Code Playgroud)
为了实际解组它,我创建了一个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);
| 归档时间: |
|
| 查看次数: |
1304 次 |
| 最近记录: |