我正在以xml类型获取响应,我想将该xml解析为Java POJO。我想从“返回”标记名开始解析。
这是xml示例
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:inquiryResponse xmlns:ns1="urn:Bakoelbuzz">
<return xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="unnamed_struct_use_soapval[15]">
<item>
<product_id xsi:type="xsd:string">P732287</product_id>
<voucher xsi:type="xsd:string">SIMPATI</voucher>
<nominal xsi:type="xsd:string">5.000</nominal>
<price xsi:type="xsd:int">5350</price>
</item>
<item>
<product_id xsi:type="xsd:string">P612697</product_id>
<voucher xsi:type="xsd:string">SIMPATI</voucher>
<nominal xsi:type="xsd:string">10.000</nominal>
<price xsi:type="xsd:int">10150</price>
</item>
</return>
</ns1:inquiryResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
这是Java中用于解析xml的代码
javax.xml.soap.MessageFactory factory = javax.xml.soap.MessageFactory.newInstance();
SOAPMessage message = factory.createMessage(
new MimeHeaders(),
new ByteArrayInputStream(xmlString.getBytes(Charset
.forName("UTF-8"))));
SOAPBody body = message.getSOAPBody();
NodeList returnNode = body.getElementsByTagName("return");
StringWriter buf = new StringWriter();
Transformer xform = TransformerFactory.newInstance().newTransformer();
xform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
xform.setOutputProperty(OutputKeys.INDENT, "yes");
xform.transform(new DOMSource(returnNode.item(0)), new StreamResult(buf));
String returnString = buf.toString();
System.out.println(returnString);
Return value = XmlJaxb.read(returnString, Return.class);
这里是Return和Item类
@XmlRootElement(name = "return")
@XmlAccessorType(XmlAccessType.FIELD)
public class Return {
@XmlElementWrapper
@XmlElement(name="item")
private List<Item> item;
}
@XmlRootElement(name = "item")
@XmlAccessorType(XmlAccessType.FIELD)
public class Item {
@XmlElement(name = "product_id")
private String productId;
@XmlElement
private String voucher;
@XmlElement
private String nominal;
@XmlElement
private int price;
}
我已经成功获得了“ return”标签。但是,当我尝试将“ return”标签转换为POJO时,项目值始终为null。请告诉我我的代码出了什么问题?
谢谢