簡體   English   中英

如何在Java中解析肥皂字符串

[英]How to parse soap string in java

我已經寫了下面的代碼來解析java中的字符串,但是沒有打印出空白消息。

如何從SOAP消息中獲取特定部分並獲取其值? 我想獲取錯誤和請求中的消息。

String xml = "<?xml version='1.0' encoding='UTF-8'?>"
             + "<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">"
             + "<S:Body>"
             + "<ns2:processResponse xmlns:ns2=\"http://ws.xxxxx.com/\">"
             + "<response><direction>response</direction>"
             + "<reference>09FG10021008111306320</reference>"
             + "<amount>0.0</amount>"
             + "<totalFailed>0</totalFailed>"
             + "<totalSuccess>0</totalSuccess>"
             + "<error>1</error>"
             + "<message>Invalid</message>"
             + "<otherReference>6360e28990c743a3b3234</otherReference>"
             + "<action>FT</action>"
             + "<openingBalance>0.0</openingBalance>"
             + "<closingBalance>0.0</closingBalance>"
             + "</response>"
             + "</ns2:processResponse>"
             + "</S:Body>"
             + "</S:Envelope>\n";

         SAXBuilder builder = new SAXBuilder();
            Reader in = new StringReader(xml);
            Document doc = null;
            Element root = null;
            Element meta = null;
            Element error = null;
            Element status_message = null;
            String status_code= "";
            String message = "";
            try
            {
             doc = builder.build(in);
             root = doc.getRootElement();
             meta = root.getChild("processResponse").getChild("response");
             error = meta.getChild("error");
             status_message = meta.getChild("message");
             status_code = error.getText();
             message = status_message.getText();

            }catch (Exception e)
             {
             // do what you want
             }
            System.out.println("status_code: " + status_code + "\nmessage: " + message);

生成的響應是status_code:消息:

在獲取xml中的元素時,您在代碼中犯了一些錯誤。 您可以使用此代碼進行檢查,

public static void main(String[] args) {
        String xml = "<?xml version='1.0' encoding='UTF-8'?>"
                + "<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">" + "<S:Body>"
                + "<ns2:processResponse xmlns:ns2=\"http://ws.xxxxx.com/\">"
                + "<response><direction>response</direction>" + "<reference>09FG10021008111306320</reference>"
                + "<amount>0.0</amount>" + "<totalFailed>0</totalFailed>" + "<totalSuccess>0</totalSuccess>"
                + "<error>1</error>" + "<message>Invalid</message>"
                + "<otherReference>6360e28990c743a3b3234</otherReference>" + "<action>FT</action>"
                + "<openingBalance>0.0</openingBalance>" + "<closingBalance>0.0</closingBalance>" + "</response>"
                + "</ns2:processResponse>" + "</S:Body>" + "</S:Envelope>\n";

        SAXBuilder builder = new SAXBuilder();
        Reader in = new StringReader(xml);
        Document doc = null;
        Element root = null;
        Element error = null;
        Element status_message = null;
        String status_code = "";
        String message = "";
        try {
            doc = builder.build(in);
            root = doc.getRootElement();
            Element body = root.getChild("Body", Namespace.getNamespace("S", "http://schemas.xmlsoap.org/soap/envelope/"));
            Element processResponse = body.getChild("processResponse", Namespace.getNamespace("ns2", "http://ws.xxxxx.com/"));
            Element response = processResponse.getChild("response");
            error = response.getChild("error");
            status_message = response.getChild("message");
            status_code = error.getText();
            message = status_message.getText();

        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("status_code: " + status_code + "\nmessage: " + message);
    }

對我來說,這給出了以下輸出,

status_code: 1
message: Invalid

將xml轉換為json,然后您可以執行任何操作。 只要記住創建一個合適的模型類來通過json映射數據

<dependency>
  <groupId>org.json</groupId>
  <artifactId>json</artifactId>
  <version>20171018</version>
</dependency>

示例java類:

import org.json.JSONObject;
import org.json.XML;

public class Main {

public static int PRETTY_PRINT_INDENT_FACTOR = 4;
public static String TEST_XML_STRING =
    "<?xml version=\"1.0\" ?><test attrib=\"moretest\">Turn this to JSON</test>";

public static void main(String[] args) {
    try {
        JSONObject xmlJSONObj = XML.toJSONObject(TEST_XML_STRING);
        String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
        System.out.println(jsonPrettyPrintString);
    } catch (JSONException je) {
        System.out.println(je.toString());
    }
}
}

json輸出:

{"test": {
"attrib": "moretest",
"content": "Turn this to JSON"
}}

暫無
暫無

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

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