Xml文件到java对象
我将xml文件转换为Java对象。然而,我遇到了一些问题 我有如下XML文件:Xml文件到java对象,java,jaxb,unmarshalling,Java,Jaxb,Unmarshalling,我将xml文件转换为Java对象。然而,我遇到了一些问题 我有如下XML文件: <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:api="http://virtualmachine.service.instancemanager.copse.jp/"> <soap:Body> <api:DetachVolumeResponse>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:api="http://virtualmachine.service.instancemanager.copse.jp/">
<soap:Body>
<api:DetachVolumeResponse>
<!--Optional:-->
<DetachVolumeResponseType>
<requestId>001</requestId>
<volumeId>a</volumeId>
<instanceId>b</instanceId>
<device>c</device>
<status>d</status>
<attachTime>2012-01-23T12:00:00Z</attachTime>
</DetachVolumeResponseType>
</api:DetachVolumeResponse>
</soap:Body>
</soap:Envelope>
程序显示错误:找不到文件,但我的路径文件绝对正确。
请告诉我为什么以及如何修复它。谢谢不要将d:\放在类路径资源中 只需将文件路径放在相对于部署的位置 目标/类别 target/classes/dir1/file1.txt
替换为该路径dir1/file1.txt您可以共享stacktrace吗?您确定该错误是由执行
新建ClassPathResource(“D:\DetachVolume.xml”)
的行引发的吗?@LutzHorn否,它被抛出:java.io.FileNotFoundException:类路径资源[D:/DetachVolume.xml]无法打开,因为它不存在,所以我知道文件路径有问题,然后确保它确实存在于类路径中。
SOAPMessage message;
try {
message = MessageFactory.newInstance().createMessage(null,
new ClassPathResource("D:\DetachVolume.xml").getInputStream());
JAXBContext jaxbContext;
jaxbContext = JAXBContext.newInstance(DetachVolumeResponseType.class);
Unmarshaller jaxbUnmarshaller;
jaxbUnmarshaller = jaxbContext.createUnmarshaller();
DetachVolumeResponseType detachVolumeResponseType = (DetachVolumeResponseType) jaxbUnmarshaller
.unmarshal(message.getSOAPBody().extractContentAsDocument());
System.out.println(detachVolumeResponseType);
} catch (IOException | SOAPException e) {
e.printStackTrace();
}catch (JAXBException e) {
e.printStackTrace();
}