我希望将此xml映射或转换为Java对象,并且我希望在xml中提取“ body”元素,并使其成为通用Java对象。我不想映射每个字段。我只想要带有所有元素的整个请求正文。我该怎么办?
<request>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header xmlns:dpwsm="http://www.reuqestpower.com/schemas/ryesbs"
xmlns:g="http://www.request.com" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<k:SoapHeader>
</k:oapHeader>
</soap:Header>
<SOAP-ENV:Body>
<purchaseOrderRequest xmlns="http://www.somecompany.com/order/PO
xmlns:addr="http://www.somecomapany.com/order/ADDR>
<firstname>Fred</firstname>
<surname>Bloggs</surname>
<addr:address">
<addr:addressLine1>2 South Road</addr:address1>
<addr:addressLine2/>
<addr:town>Colchester</addr:town>
<addr:county>Essex</addr:county>
<addr:postcode>CO8 9SR</addr:postcode>
</addr:address>
<telephone>01334 234567</po:telephone>
</purchaseOrderRequest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
</request>
答案 0 :(得分:0)
您可以使用DOM Parser,然后检查节点:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(new InputSource(new StringReader(xml)));
Node body = dom.getDocumentElement().getElementsByTagName("SOAP-ENV:Body").item(0);
for(int i=0;i<body.getChildNodes().getLength();i++)
{
System.out.println(body.getChildNodes().item(i));
}
答案 1 :(得分:0)
如果可以或想使用第三方库,则可以使用Jsoup
:
org.jsoup.nodes.Document dom = Jsoup.parse(xml, "", Parser.xmlParser());
Element body = dom.getElementsByTag("SOAP-ENV:Body").get(0);
for(Element child:body.children())
{
System.out.println(child);
}