簡體   English   中英

Xml文件到Java對象

[英]Xml file to java object

我將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>
         <!--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>

這是我的代碼:

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

程序顯示錯誤,找不到文件,但是我的路徑文件絕對正確。 請告訴我原因以及如何解決。 謝謝

不要將d:\\放在classpathresource中。

只需將文件路徑相對於您的部署。

目標/類目標/類/dir1/file1.txt

替換為此路徑dir1 / file1.txt

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM