我正在使用Jdom 2.0.5并打开一个文件one.xml,其中包含另一个文件two.xml到xinclude。我按如下方式调用构建器:
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 =&#34; web-servers&#34;应该使用xml:id =&#34; web-servers&#34;来获取元素web-serve,但事实并非如此。有什么建议吗?
答案 0 :(得分:1)
Xerces只支持xpointer element()Scheme
所以也许
<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”元素
答案 1 :(得分:0)
如果您想使用xpointer,请尝试使用:http://etourdot.github.io/xincproc/