skip to Main Content

i’m looking for a parser or SOAP client in Java to convert XML(WSDL) from Magento SOAP v1 API to JSON Object.

Magento SOAP v1 API returns a XML which looks like this:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:Magento"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                   xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
                   SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
    <ns1:callResponse>
        <callReturn xsi:type="ns2:Map">
            <item>
                <key xsi:type="xsd:string">store_id</key>
                <value xsi:type="xsd:string">1</value>
            </item>
            <item>
                <key xsi:type="xsd:string">created_at</key>
                <value xsi:type="xsd:string">2013-03-05 05:56:35</value>
            </item>
            <item>
                <key xsi:type="xsd:string">updated_at</key>
                <value xsi:type="xsd:string">2017-11-09 15:37:05</value>
            </item>
            <item>
                    <key xsi:type="xsd:string">shipping_address</key>
                    <value xsi:type="ns2:Map">
                        <item>
                            <key xsi:type="xsd:string">address_id</key>
                            <value xsi:type="xsd:string">1</value>
                        </item>
                        <item>
                            <key xsi:type="xsd:string">created_at</key>
                            <value xsi:type="xsd:string">2013-01-31 11:37:38</value>
                        </item>
                        <item>
                            <key xsi:type="xsd:string">updated_at</key>
                            <value xsi:type="xsd:string">2017-11-09 15:37:05</value>
                        </item>
                    </value>
           </item>
        </callReturn>
    </ns1:callResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

and i would like to received a simple JSON Object like this:

{
  "store_id": "1",
  "created_at": "2013-03-05 05:56:35",
  "updated_at": "2017-11-09 15:37:05",
  "shipping_address": {
    "address_id": "1",
    "created_at": "2013-01-31 11:37:38",
    "updated_at": "2017-11-09 15:37:05"
  }
}

3

Answers


  1. you may use this lib https://github.com/stleary/JSON-java/blob/master/src/main/java/org/json/XML.java

    then parse parts of SOAP Response to get json data this way:

    import org.json.JSONObject;
    import org.json.XML;
    
    public class Main {
    
        public static void main(String[] args) {
            try {
    
                JSONObject jsonObj = XML.toJSONObject(XML_STRING);
    
            } catch (JSONException e) {
                System.out.println(e.toString());
            }
        }
    }
    
    Login or Signup to reply.
  2. If you are using Java 8 or later, you should check out my open source library: unXml. unXml basically maps from Xpaths to Json-attributes.

    It’s available on Maven Central.

    Example

    import com.fasterxml.jackson.databind.node.ObjectNode;
    import com.nerdforge.unxml.factory.ParsingFactory;
    import com.nerdforge.unxml.parsers.Parser;
    import org.w3c.dom.Document;
    
    public class Parser {
      public ObjectNode parseXml(String xml){
        Parsing parsing = ParsingFactory.getInstance().create();
        Document document = parsing.xml().document(xml);
    
        Parser<ObjectNode> parser = parsing.obj()
          .attribute("store_id", "//item[key/text() = 'store_id']/value")
          .attribute("created_at", "//item[key/text() = 'created_at']/value")
          .attribute("updated_at", "//item[key/text() = 'updated_at']/value")
          .attribute("shipping_address", parsing.obj("//item[key/text() = 'shipping_address']")
              .attribute("address_id", "value/item[key/text() = 'address_id']/value")
              .attribute("created_at", "value/item[key/text() = 'created_at']/value")
              .attribute("updated_at", "value/item[key/text() = 'updated_at']/value")
          )
          .build();
    
        ObjectNode result = parser.apply(document);
        return result;
      }
    }
    

    It will return a Jackson ObjectNode, with the following json:

    {
      "created_at": "2013-03-05 05:56:35",
      "shipping_address": {
        "created_at": "2013-01-31 11:37:38",
        "address_id": "1",
        "updated_at": "2017-11-09 15:37:05"
      },
      "store_id": "1",
      "updated_at": "2017-11-09 15:37:05"
    }
    
    Login or Signup to reply.
  3.    
        public static JSONObject readToJSONObject(String xmlBody) {
            SAXReader saxReader = new SAXReader();
            try {
                Document document = saxReader.read(new ByteArrayInputStream(xmlBody.getBytes()));
                Element rootElement = document.getRootElement();
    
                JSONObject jsonObject = new JSONObject();
    
                doParse(jsonObject, rootElement);
    
                return jsonObject;
            } catch (DocumentException e) {
                e.printStackTrace();
                return null;
            }
        }
    
        public static void doParse(JSONObject jsonObject, Element element) {
            String elementName = element.getName();
            List<Element> elements = element.elements();
            if (CollectionUtils.isEmpty(elements)) {
                jsonObject.put(elementName, element.getText());
            } else {
                JSONObject itemJsonObject = new JSONObject();
                for (Element itemElement : elements) {
                    doParse(itemJsonObject, itemElement);
                }
                jsonObject.put(elementNmae, itemJsonObject);
            }
        }
    

    use

    readToJSONObject(xmlBody)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search