簡體   English   中英

如何將此XML SOAP響應解析為POJO?

[英]How to parse this XML SOAP response to a POJO?

我在將XML SOAP返回到相應的POJO類時遇到問題。 XML返回如下所示:

    <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Header></env:Header>
    <env:Body>
        <ns2:teste xmlns:ns2="http://teste.com.br/">
            <retorno>
                <codigoRetorno>000</codigoRetorno>
                <descricao>Consulta Realizada com Sucesso</descricao>
                <item>
                    <a>teste</a>
                    <b>teste</b>
                    <c>teste</c>
                </item>
                <item>
                    <a>teste</a>
                    <b>teste</b>
                    <c>teste</c>
                </item>
            </retorno>
        </ns2:teste >
    </env:Body>
</env:Envelope>

我嘗試使用Jackson XMLmapper,但我不能讓它在反序列化期間將'RETURN'節點視為ROOT元素。 它將'Envelope'節點視為ROOT節點。

我只需要提取返回節點並轉換為我的pojo類。

另一個問題是'item'節點應該是集合的一部分,但是沒有父節點對這些元素進行分組。

有沒有人知道解析這種類型的xml的解析器?

您可以通過以下方式合並流式XML分析器(StAX)和XmlMapper

import java.io.StringReader;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

public class Deser {
    // @formatter:off

    private static final String JSON = "    <env:Envelope xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" + 
            "    <env:Header></env:Header>\n" + 
            "    <env:Body>\n" + 
            "        <ns2:teste xmlns:ns2=\"http://teste.com.br/\">\n" + 
            "            <retorno>\n" + 
            "                <codigoRetorno>000</codigoRetorno>\n" + 
            "                <descricao>Consulta Realizada com Sucesso</descricao>\n" + 
            "                <item>\n" + 
            "                    <a>teste</a>\n" + 
            "                    <b>teste</b>\n" + 
            "                    <c>teste</c>\n" + 
            "                </item>\n" + 
            "                <item>\n" + 
            "                    <a>teste</a>\n" + 
            "                    <b>teste</b>\n" + 
            "                    <c>teste</c>\n" + 
            "                </item>\n" + 
            "            </retorno>\n" + 
            "        </ns2:teste >\n" + 
            "    </env:Body>\n" + 
            "</env:Envelope>";

    // @formatter:on

    private static final String TARGET_ELEMENT = "retorno";

    public static void main(String[] args) throws Exception {
        XmlMapper xmlMapper = new XmlMapper();
        xmlMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

        XMLInputFactory f = XMLInputFactory.newFactory();
        XMLStreamReader sr = f.createXMLStreamReader(new StringReader(JSON));

        while (sr.hasNext()) {
            int type = sr.next();

            if (type == XMLStreamReader.START_ELEMENT && TARGET_ELEMENT.equals(sr.getLocalName())) {
                Retorno r = xmlMapper.readValue(sr, Retorno.class);

                System.out.println(r.getDescricao());
            }
        }
    }
}

class Retorno {
    private int codigoRetorno;
    private String descricao;

    public int getCodigoRetorno() {
        return codigoRetorno;
    }

    public void setCodigoRetorno(int codigoRetorno) {
        this.codigoRetorno = codigoRetorno;
    }

    public String getDescricao() {
        return descricao;
    }

    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }
}

這會產生:

Consulta Realizada com Sucesso

根據需要調整代碼,這只是為了證明如何讓它做你需要的!

我發現最干凈的解決方案是使用JSOUP:

private <T> T parseResponse(HttpEntity entity, Class<T> typeTarget) throws Exception {

       try {

           String xmlSoapResponse = EntityUtils.toString(entity, StandardCharsets.UTF_8);
           String xmlRetorno = extractXmlElement(xmlSoapResponse, "retorno");

           XmlMapper xmlMapper = new XmlMapper();
           xmlMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
           xmlMapper.registerModule(new JavaTimeModule());
           return xmlMapper.readValue(xmlRetorno.toString(), typeTarget);

       } catch (Exception e) {
           throw new Exception("Fail during parser", e);
       }

   }
private String extractXmlElement(String xmlString, String nodeTagNameElement) {

        Document document = Jsoup.parse(xmlString, "", Parser.xmlParser());
        document.outputSettings().prettyPrint(false);
        Elements retorno = document.getElementsByTag(nodeTagNameElement);

        return retorno.toString();

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM