我下面有一个名为“TheFile.xml”的 xml
<!DOCTYPE page [
<!ENTITY class_common SYSTEM "class_common.xml" >]>
<api xmlns:denp="http://intranet.denali.com/wiki/DocBook_profiling">
&class_common;
正如您所看到的,上面的行引用了另一个名为“class_common.xml”的 xml 文件。然而,当我这样做时
cl = class_xml.ObjectFactory.class.getClassLoader();
context = JAXBContext.newInstance("class_xml", cl);
Unmarshaller u = context.createUnmarshaller();
api = (Api) u.unmarshal(new FileInputStream(classXMLFile));
我收到一些如下错误。我错过了什么吗?谢谢
线程“main”中出现异常 javax.xml.bind.UnmarshalException - 带有链接异常:
[org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 17; External Entity: Failed to read external document 'class_common.xml', because 'file' access is not allowed due to restriction set by the accessExternalDTD property.]
最佳答案
好的,这就是答案,Java 代码需要如下
cl = class_xml.ObjectFactory.class.getClassLoader();
context = JAXBContext.newInstance("class_xml", cl);
Unmarshaller u = context.createUnmarshaller();
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setXIncludeAware(true);
spf.setNamespaceAware(true);
spf.setValidating(true); // Not required for JAXB/XInclude
XMLReader xr = (XMLReader) spf.newSAXParser().getXMLReader();
SAXSource source = new SAXSource(xr, new InputSource(new
FileInputStream(classXMLFile)));
api = (Api) u.unmarshal(source);
关于JAXB 使用包含内容解码 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24460892/