2017-07-28 7 views
1

私は以下のSOAP応答を持っています。私はSOAPメッセージを繰り返し処理し、JSONArray形式のlistMetadataResponseタグでデータを取得したいと考えています。ここに私のサンプルSOAPレスポンスです:私はJSON.Soでキーと値のペアとして各属性ノードと値を持つJSONObjectとして結果ノードのそれぞれを取得したいSOAP応答をJSONArrayに変換するには

<?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> 

、この場合には、私は結果が欲しいです2つの結果JSONObjectを含むJSONArray

私はこのコードを試しました。私はノード名を取得していますが、ノード値を取得していません。

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

stonaryでJSON-javaライブラリを使用できます。

次のコードを使用して、XML文字列をJSONObjectに変換できます。

JSONObject data = XML.toJSONObject(xmlString);

あなたがここでそれについての詳細情報を見つけることができます:上記参照してJSON-java

+0

は '私はすべての値は、彼らが整数である場合には文字列であることを行うことができます方法は?JSONObjectのキーセットを使用してオブジェクトの上@Chinmayジャイナ –

+0

反復あり、その後、'持つInteger.toString(int)を使用します –

+0

私はそれについて考えましたが、私のデータは本当に巨大なので、それは私のために大いにあるようです。とにかくおかげさまで、私はループを使って頑張らなければならないと思います。 –

0

を、私はこれは同様に他の人のために働くことを願っていますleast.Iでソリューションを実装することができています。

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; 
}