我希望将此 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>
最佳答案
您可以使用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));
}
关于java - 我们如何在java中使用JAXB创建通用对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56238192/