【发布时间】:2014-08-09 18:21:59
【问题描述】:
我是unmarshalling,我的本地文件系统中存在一些使用JAXB 的XML 文件。
只要源 XML 中没有任何 ENTITY 声明,unmarshalling 就可以了。但是其中的一些 XML 文件使用相对路径导入了一些带有 DOCTYPE ENTITY 声明的 XML 文件。这会导致JAXB 无法尝试定位导入的文件,因为相对路径与当前类路径绝对没有连接,因此会出现FileNotFoundExceptions。我该如何解决这个问题?
FWIW,我的类路径中的 XML 中的复杂类型的模型 JAVA 对象等价物(如下面使用的 Root 类)作为使用 maven-jaxb-plugin 生成的 jar。
XML 文件中的 DOCTYPE 声明:
<!DOCTYPE doc [
<!ENTITY decorator SYSTEM "relative/path/to/another/xmlfile">
]>
我用来解组 XML 的代码:
JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
JAXBElement<Root> unmarshalledObject = (JAXBElement<Root>) unmarshaller
.unmarshal(new FileInputStream(new File("/absolute/path/to/my/xmlfile")));
Root rootNode = unmarshalledObject.getValue();
【问题讨论】:
标签: java xml jaxb unmarshalling