如何解析 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