将SOAP响应转换为JSONArray

4
我有以下SOAP响应。我想遍历SOAP消息并获取JSONArray格式的listMetadataResponse标记中的数据。这是我的示例SOAP响应:
 <?xml version="1.0" encoding="UTF-8"?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://soap.sforce.com/2006/04/metadata">
       <soapenv:Body>
          <listMetadataResponse>
             <result>
                <createdById>00528000001m5RRAAY</createdById>
                <createdByName>Hariprasath Thanarajah</createdByName>
                <createdDate>1970-01-01T00:00:00.000Z</createdDate>
                <fileName>objects/EmailMessage.object</fileName>
                <fullName>EmailMessage</fullName>
                <id />
                <lastModifiedById>00528000001m5RRAAY</lastModifiedById>
                <lastModifiedByName>Hariprasath Thanarajah</lastModifiedByName>
                <lastModifiedDate>1970-01-01T00:00:00.000Z</lastModifiedDate>
                <namespacePrefix />
                <type>CustomObject</type>
             </result>
              <result>
                <createdById>00528000001m5RRAAY</createdById>
                <createdByName>Hariprasath Thanarajah</createdByName>
                <createdDate>1970-01-01T00:00:00.000Z</createdDate>
                <fileName>objects/EmailMessage.object</fileName>
                <fullName>EmailMessage</fullName>
                <id />
                <lastModifiedById>00528000001m5RRAAY</lastModifiedById>
                <lastModifiedByName>Hariprasath Thanarajah</lastModifiedByName>
                <lastModifiedDate>1970-01-01T00:00:00.000Z</lastModifiedDate>
                <namespacePrefix />
                <type>CustomObject</type>
             </result>
          </listMetadataResponse>
       </soapenv:Body>
    </soapenv:Envelope>

我希望将每个结果节点作为JSONObject获取,其中每个属性节点和值作为JSON中的键值对。因此,在这种情况下,我希望将结果作为JSONArray返回,并在其中包含两个结果JSONObject。
我尝试了这段代码,我能够获取节点名称,但无法获取节点值。
private static Document loadXMLString(String response) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    InputSource is = new InputSource(new StringReader(response));

    return db.parse(is);
}

public static JSONArray getFullData(String tagName, String request) throws Exception {
    JSONArray resultArray = new JSONArray();
    Document xmlDoc = loadXMLString(request);
    NodeList nodeList = xmlDoc.getElementsByTagName("*");
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getNodeName().equals("result")) {
                JSONObject rootObject = new JSONObject();
                NodeList childNodeList = nodeList.item(i).getChildNodes();
                for (int j = 0; j < childNodeList.getLength(); j++) {
                    node = childNodeList.item(i);
                    rootObject.put(node.getNodeName(), node.getNodeValue());
                }
                resultArray.put(rootObject);
            }
        }
    }
}
2个回答

3

您可以使用stleary提供的JSON-java库。

您可以使用以下代码将XML字符串转换为JSONObject。

JSONObject data = XML.toJSONObject(xmlString);

您可以在此处找到有关其更多信息:JSON-java


有没有办法在值为整数的情况下将所有的值都转换为字符串?@Chinmay Jain - Young Emil
使用JSONObject的keyset迭代对象,然后使用Integer.toString(int) - Chinmay jain
我考虑过那个方法,但对我来说似乎太复杂了,因为我的数据非常庞大。无论如何,还是谢谢你,我想我只能努力使用循环了。 - Young Emil

2

通过以上参考,我至少能够实现该解决方案。我希望这对其他人也有用。

private static JSONObject extractData(NodeList nodeList, String tagName) throws TransformerConfigurationException,
        TransformerException, TransformerFactoryConfigurationError, JSONException {
    JSONObject resultObject = new JSONObject();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (!node.getNodeName().equals(tagName) && node.hasChildNodes()) {
            return extractData(node.getChildNodes(), tagName);
        } else if (node.getNodeName().equals(tagName)) {
            DOMSource source = new DOMSource(node);
            StringWriter stringResult = new StringWriter();
            TransformerFactory.newInstance().newTransformer().transform(source, new StreamResult(stringResult));
            resultObject = XML.toJSONObject(stringResult.toString()).optJSONObject(tagName);
        }
    }
    return resultObject;
}

public static JSONObject getFullData(String tagName, SOAPMessage message) throws Exception {
    NodeList nodeList = message.getSOAPBody().getChildNodes();
    JSONObject resultObject = extractData(nodeList, tagName);
    return resultObject;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接