如何将XML SOAP响应解析为POJO?

时间:2019-05-07 23:33:19

标签: java xml parsing soap jackson

将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元素。会将“信封”节点视为ROOT节点。

我只需要提取返回节点并转换为我的pojo类。

另一个问题是“ item”节点应该是集合的一部分,但是没有将这些元素分组的父节点。

有人知道解析器会对这种类型的xml进行反序列化吗?

2 个答案:

答案 0 :(得分:0)

您可以通过以下方式合并流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;
    }
}

这将产生:

  

Concessult Realizada com Sucesso

根据需要修改代码,这只是为了证明如何使它完成所需的工作!

答案 1 :(得分:0)

我找到的最干净的解决方案是使用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();

    }