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

时间:2018-10-02 09:36:57

标签: java soap jdom jdom-2

我已经编写了以下代码来解析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: 讯息:

2 个答案:

答案 0 :(得分:1)

在获取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

答案 1 :(得分:0)

将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"
}}