<?xml version="1.0" encoding="utf-8"?>\r\n" + "<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">\r\n" + " <soap:Body>\r\n" + " <SendOTTResponse xmlns="http://tempuri.org/">\r\n" + " <SendOTTResult>\r\n" + " <ResCode>0</ResCode>\r\n" + " <ResDesc>Sent</ResDesc>\r\n" + " </SendOTTResult>\r\n" + " </SendOTTResponse>\r\n" + " </soap:Body>\r\n" + "</soap:Envelope>" + "</soap:Envelope>
Parse xml soap to object
Hi all, em có 1 soap như này và muốn convert sang object, mn giúp em
Remain: 5
2 Answers
monkey
Beginner
monkey
Beginner
Đây em:
public class SendOTTResult { public int ResCode; public String ResDesc; } public class SendOTTResponse { public SendOTTResult SendOTTResult; }
-
-1
monkey
Beginner
monkey
Beginner
Đây em:
package com.example.stackoverflow; import java.io.StringReader; import javax.xml.bind.JAXBContext; import javax.xml.bind.Unmarshaller; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import lombok.Data; public class JAXBExample { public static void main(String[] args) throws Exception { String xml = <span>"<?xml version=<span>"</span>1.0<span>"</span> encoding=<span>"</span>utf-8<span>"</span>?><span>\n</span>"</span> + <span>"<soap:Envelope xmlns:soap=<span>"</span>http://www.w3.org/2003/05/soap-envelope<span>"</span> xmlns:xsi=<span>"</span>http://www.w3.org/2001/XMLSchema-instance<span>"</span> xmlns:xsd=<span>"</span>http://www.w3.org/2001/XMLSchema<span>"</span>><span>\n</span>"</span> + <span>" <soap:Body><span>\n</span>"</span> + <span>" <SendOTTResponse xmlns=<span>"</span>http://tempuri.org/<span>"</span>><span>\n</span>"</span> + <span>" <SendOTTResult><span>\n</span>"</span> + <span>" <ResCode>0</ResCode><span>\n</span>"</span> + <span>" <ResDesc>Sent</ResDesc><span>\n</span>"</span> + <span>" </SendOTTResult><span>\n</span>"</span> + <span>" </SendOTTResponse><span>\n</span>"</span> + <span>" </soap:Body><span>\n</span>"</span> + <span>"</soap:Envelope>"</span>; JAXBContext jaxbContext = JAXBContext.newInstance( Envelope.class, Body.class, SendOTTResponse.class, SendOTTResult.class ); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); System.out.println(unmarshaller.unmarshal(new StringReader(xml))); } @Data public static class SendOTTResult { @XmlElement(name = "ResCode", namespace="http://tempuri.org/") public int ResCode; @XmlElement(name = "ResDesc", namespace="http://tempuri.org/") public String ResDesc; } @Data public static class SendOTTResponse { @XmlElement(name = "SendOTTResult", namespace="http://tempuri.org/") public SendOTTResult SendOTTResult; } @Data public static class Body { @XmlElement(name = "SendOTTResponse", namespace="http://tempuri.org/") public SendOTTResponse SendOTTResponse; } @Data @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement(name="Envelope", namespace="http://www.w3.org/2003/05/soap-envelope") public static class Envelope { @XmlElement(name = "Body", namespace="http://www.w3.org/2003/05/soap-envelope") public Body Body; } }
-
0