我使用Jdom 2.0.5并打开文档one.xml,其中通过xinclude包含另一个文件two.xml。我像下面这样调用构建器:
SAXBuilder builder = new SAXBuilder();
builder.setJDOMFactory(new LocatedJDOMFactory());
builder.setFeature("http://apache.org/xml/features/xinclude", true);
Document doc = builder.build(new File("c:\temp\one.xml"));
XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
System.out.println("salida es "+out.outputString(doc));
one.xml文件如下:
<?xml version="1.0" encoding="UTF-8" ?>
<eco xmlns:xi="http://www.w3.org/2001/XInclude">
<tmp-path>/opt/eco/ecoprd/tmp</tmp-path>
<!--
<xi:include href="two.xml" parse="xml"/>
-->
<xi:include href="two.xml" parse="xml" xpointer="web-servers"/>
</eco>
和two.xml的格式如下:
<?xml version="1.0" encoding="UTF-8" ?>
<eco>
<web-servers xml:id="web-servers">
<valuations>
<url-base>http://naboo:8080</url-base>
<context>/Statements</context>
<path>/services/ValuationsXMLGenerator</path>
</valuations>
</web-servers>
</eco>
包含没有xpointer的文件工作得很好。但是如果我使用xpointer,我会得到如下错误:
org.jdom2.input.JDOMParseException: Error on line 7 of document file:/c:/temp/one.xml: An 'include' failed, and no 'fallback' element was found.
at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:228)
at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:277)
at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:264)
at org.jdom2.input.SAXBuilder.build(SAXBuilder.java:1116)
at test.XMLTest.main(XMLTest.java:17)
Caused by: org.xml.sax.SAXParseException; systemId: file:/c:/temp/one.xml; lineNumber: 7; columnNumber: 65; An 'include' failed, and no 'fallback' element was found.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.xinclude.XIncludeHandler.reportError(Unknown Source)
at org.apache.xerces.xinclude.XIncludeHandler.reportFatalError(Unknown Source)
at org.apache.xerces.xinclude.XIncludeHandler.reportFatalError(Unknown Source)
at org.apache.xerces.xinclude.XIncludeHandler.reportFatalError(Unknown Source)
at org.apache.xerces.xinclude.XIncludeHandler.emptyElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:217)
... 4 more
我错过了什么?假定xpointer="web-servers"应该得到元素web-servers与xml:id="web-servers",但它不是。有什么建议吗?
Xerces只支持xpointer元素()Scheme
- https://xerces.apache.org/xerces2-j/javadocs/xerces2/org/apache/xerces/xpointer/XPointerHandler.html
- http://www.w3.org/TR/2003/REC-xptr-element-20030325/
所以可能
<xi:include href="two.xml" parse="xml" xpointer="element(web-servers)"/>
或
<xi:include href="two.xml" parse="xml" xpointer="element(/1/1)"/>
,第一个/1是"eco"元素第二个/1是"web-servers"元素
如果您想使用xpointer,请尝试使用:http://etourdot.github.io/xincproc/