简体   繁体   中英

Xml file to java object

I convert xml file to Java object. However, i got some problems.

I have XML file like this:

<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.

Just put the filepath relative to your deployment.

target/classes target/classes/dir1/file1.txt

replace with this path dir1/file1.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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