繁体   English   中英

Soap Response XML to Json - spring boot

[英]Soap Response XML to Json - spring boot

我在我的项目中使用 WSDL 服务,返回的响应是 XML。 对于我的应用程序,我需要以 JSON 传输响应。 这里使用的框架是spring boot。 编码语言很时髦

我试图将 xml 转换为 Json,但不需要响应。

来自 WSDL 的 Soap 响应

<soapenv:Envelope   xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <RESP xmlns="http://xmlns.oracle.com/Enterprise/Tools/schemas/RESP">
            <Data>
                <Resp_Data>
                    <ID >123456789</ID>
                    <Name >Downey jr,Robert John</Name>
                    <LastName >Downey jr</LastName>
                    <FirstName >Robert</FirstName>
                    <MiddleName >John</MiddleName>
                    <Movies>
                        <MName>Sherlock Homes</MName>
                        <Year>2009</Year>
                        <Length>2h 8m</Length>
                        <BoxOffice>$208,711,166</BoxOffice>
                        <Ratings>
                            <ImdbRaring>7.6</ImdbRaring>
                            <RottenTomatoes>70%</RottenTomatoes>
                        </Ratings>
                    <Movies>
                    <Movies>
                        <MName>Iron Man</MName>
                        <Year>2008</Year>
                        <Length>2h 6m</Length>
                        <BoxOffice>$318,298,180</BoxOffice>
                        <Ratings>
                            <ImdbRaring>7.9</ImdbRaring>
                            <RottenTomatoes>93%</RottenTomatoes>
                        </Ratings>
                    <Movies>
                    <Movies>
                        <MName>Iron Man2</MName>
                        <Year>2008</Year>
                        <Length>2h 4m</Length>
                        <BoxOffice>$312,057,433</BoxOffice>
                        <Ratings>
                            <ImdbRaring>7.0</ImdbRaring>
                            <RottenTomatoes>73%</RottenTomatoes>
                        </Ratings>
                    <Movies>
                </Resp_Data>
            </Data>
        <RESP>
    </soapenv:Body>
</soapenv:Envelope>

我尝试使用 XML.toJSONObject(xmlString),转换后的 Json 响应是

{
    "Envelope": {
        "Body": {
            "RESP": {
                "Data": {
                    "Resp_Data": {
                        "ID": "123456789",
                        "Name": "Downey jr,Robert John",
                        "LastName": "Downey jr",
                        "FirstName": "Robert",
                        "MiddleName": "John",
                        "Movies": [
                            {
                                "MName": "Sherlock Homes",
                                "Year": "2009",
                                "Length": "2h 8m",
                                "BoxOffice": "$208,711,166",
                                "Ratings": {
                                    "ImdbRaring": "7.6",
                                    "RottenTomatoes": "70%"
                                }
                            },
                            {
                                "MName": "Iron Man",
                                "Year": "2008",
                                "Length": "2h 6m",
                                "BoxOffice": "$318,298,180",
                                "Ratings": {
                                    "ImdbRaring": "7.9",
                                    "RottenTomatoes": "93%"
                                }
                            },
                            {
                                "MName": "Iron Man2",
                                "Year": "2008",
                                "Length": "2h 4m",
                                "BoxOffice": "$312,057,433",
                                "Ratings": {
                                    "ImdbRaring": "7.0",
                                    "RottenTomatoes": "73%"
                                }
                            }
                        ]
                    }
                },
                "_xmlns": "http://xmlns.oracle.com/Enterprise/Tools/schemas/RESP"
            },
            "__prefix": "soapenv"
        },
        "_xmlns:soapenv": "http://schemas.xmlsoap.org/soap/envelope/",
        "_xmlns:soapenc": "http://schemas.xmlsoap.org/soap/encoding/",
        "_xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
        "_xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
        "__prefix": "soapenv"
    }
}

另外,我尝试使用 groovy 的 Json Builder 来解析 XML 并从它创建 Json。 下面是代码

def responseXML = new XmlSlurper().parseText(xmlString)
                def builder = new groovy.json.JsonBuilder()
                builder {
                    responseXML.Body.RESP.Data.Resp_Data.each { nodeElem ->
                        nodeElem.childNodes().each { childElem ->
                            "$childElem.name" childElem.text()
                        }
                    }
                }

我得到的 Json 响应是

{
    "ID": "123456789",
    "Name": "Downey jr,Robert John",
    "LastName": "Downey jr",
    "FirstName": "Robert",
    "MiddleName": "John",
     "W_CHKLST_WS_REC": "Sherlock Homes 2009 2h 8m $208,711,166 7.6 70% Iron Man 2008 2h 6m $318,298,180 7.9 93% Iron Man2 2008 2h 4m $312,057,433 7.0 73%"
}

我正在尝试实现 Json 响应

{
"ID": "123456789",
"Name": "Downey jr,Robert John",
"LastName": "Downey jr",
"FirstName": "Robert",
"MiddleName": "John",
"Movies": [
    {
        "MName": "Sherlock Homes",
        "Year": "2009",
        "Length": "2h 8m",
        "BoxOffice": "$208,711,166",
        "Ratings": {
            "ImdbRaring": "7.6",
            "RottenTomatoes": "70%"
        }
    },
    {
        "MName": "Iron Man",
        "Year": "2008",
        "Length": "2h 6m",
        "BoxOffice": "$318,298,180",
        "Ratings": {
            "ImdbRaring": "7.9",
            "RottenTomatoes": "93%"
        }
    },
    {
        "MName": "Iron Man2",
        "Year": "2008",
        "Length": "2h 4m",
        "BoxOffice": "$312,057,433",
        "Ratings": {
            "ImdbRaring": "7.0",
            "RottenTomatoes": "73%"
        }
    }
]
}

Underscore-java 库可以将 xml 转换为 json

import com.github.underscore.lodash.U;

public class JsonConversion {
    public static String TEST_XML_STRING =
        "<?xml version=\"1.0\" ?><test attrib=\"moretest\">Turn this to JSON</test>";
    public static void main(String args[]) {
        String jsonPrettyPrintString = U.xmlToJson(TEST_XML_STRING);
        System.out.println(jsonPrettyPrintString);
        // {
        //   "test": {
        //     "-attrib": "moretest",
        //     "#text": "Turn this to JSON"
        //   }
        // }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM