我有以下XML文件(SOAP响应),我正在尝试映射到Java对象:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:AffaireByTiersResponse xmlns:ns2="http://service.hibernate.com/">
<Affaire>
<code_produit>Cred</code_produit>
<id>1</id>
<montant_fin>2000.0</montant_fin>
<id_tier>1</id_tier>
</Affaire>
<Affaire>
<code_produit>Cred</code_produit>
<id>2</id>
<montant_fin>25000.0</montant_fin>
<id_tier>1</id_tier>
</Affaire>
</ns2:AffaireByTiersResponse>
</soap:Body>
</soap:Envelope>
为了对文件进行编组,我需要仅将标记<AffaireByTiersResponse>
保留为根元素,并将其名称更改为<Affaires>
。
这样做的最佳方式是什么?
答案 0 :(得分:0)
可以使用javax.xml.soap.MessageFactory
来完成从SOAP信封中提取底层内容。
String example = new String(Files.readAllBytes(Paths.get("input.xml")), StandardCharsets.UTF_8);
SOAPMessage message = MessageFactory.newInstance().createMessage(null,
new ByteArrayInputStream(example.getBytes()));
Document doc = message.getSOAPBody().extractContentAsDocument();
Element root = doc.getDocumentElement();
然后可以重命名根节点
doc.renameNode(root, root.getNamespaceURI(), "Affaires");
然后可以将此文档传递给JAXB unmarshaller
Unmarshaller unmarshaller = JAXBContext.newInstance(Affaires.class).createUnmarshaller();
Affaires farm = (Affaires) unmarshaller.unmarshal(doc);