简体   繁体   English

Xml文件到Java对象

[英]Xml file to java object

I convert xml file to Java object. 我将xml文件转换为Java对象。 However, i got some problems. 但是,我遇到了一些问题。

I have XML file like this: 我有这样的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>
         <!--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>

This is my code: 这是我的代码:

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();
        }

The program show error that file not found but my path file is absolutely right. 程序显示错误,找不到文件,但是我的路径文件绝对正确。 Please tell me why and how to fix it. 请告诉我原因以及如何解决。 Thanks 谢谢

Dont put d:\\ in the classpathresource. 不要将d:\\放在classpathresource中。

Just put the filepath relative to your deployment. 只需将文件路径相对于您的部署。

target/classes target/classes/dir1/file1.txt 目标/类目标/类/dir1/file1.txt

replace with this path dir1/file1.txt 替换为此路径dir1 / file1.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM