0

I am trying to Convert my XML in to JSONObject and JSONArray, i want retrieve child nodes (example <ns2:make>) from JSONObject or JSONArray, can someone please help me how to read data from child nodes.

String TEST_XML_STRING ="<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">"+
            "<S:Header/>"+
            "<S:Body>"+
                "<ns7:NewPORequest xmlns:ns2=\"http://services.m.com/ement/common\""+
                    "xmlns:ns5=\"http://services.m.com/ement/po\" xmlns:ns7=\"http://services.m.com/ementServices/ws\">"+
                    "<ns7:tracingLevel>OFF</ns7:tracingLevel>"+
                    "<ns7:userId>TestUtil</ns7:userId>"+
                    "<ns7:applicationId></ns7:applicationId>"+
                    "<ns7:userType>Buyer</ns7:userType>"+
                    "<ns5:PurchaseOrder>"+
                        "<ns5:poExternalId>XXX-930220</ns5:poExternalId>"+
                        "<ns5:repairOrderNumber>1234</ns5:repairOrderNumber>"+
                        "<ns5:estimateDetails>"+
                            "<ns2:estimatorFirstName></ns2:estimatorFirstName>"+
                            "<ns2:estimatorLastName></ns2:estimatorLastName>"+
                            "<ns2:estimateVersion>E1</ns2:estimateVersion>"+
                        "</ns5:estimateDetails>"+
                        "<ns5:quoteId>52452</ns5:quoteId>"+
                        "<ns5:supplierQuoteNumber>118596</ns5:supplierQuoteNumber>"+
                        "<ns5:documentName>Test_PO_1</ns5:documentName>"+
                        "<ns5:documentStatus>Submitted</ns5:documentStatus>"+
                        "<ns5:insuranceCompany>"+
                            "<ns2:VantiveCode>FA</ns2:VantiveCode>"+
                        "</ns5:insuranceCompany>"+
                        "<ns5:claimNumber></ns5:claimNumber>"+
                        "<ns5:shipToLocation>"+
                            "<ns2:address>"+
                                "<ns2:streetAddress></ns2:streetAddress>"+
                                "<ns2:streetAddress2>Suit 900</ns2:streetAddress2>"+
                                "<ns2:city></ns2:city>"+
                                "<ns2:stateCode>IL</ns2:stateCode>"+
                                "<ns2:zip>60654</ns2:zip>"+
                            "</ns2:address>"+
                            "<ns2:locationType>NotApplicable</ns2:locationType>"+
                        "</ns5:shipToLocation>"+
                        "<ns5:repairFacilityLocation>"+
                            "<ns2:repairFacilityID>4465</ns2:repairFacilityID>"+
                        "</ns5:repairFacilityLocation>"+
                        "<ns5:supplierLocation>"+
                            "<ns2:supplierId>5000</ns2:supplierId>"+
                        "</ns5:supplierLocation>"+
                        "<ns5:vehicleInfo>"+
                            "<ns2:vehicleOptionsMapCode>option map code"+
                            "</ns2:vehicleOptionsMapCode>"+
                            "<ns2:year>2006</ns2:year>"+
                            "<ns2:make>Nissan</ns2:make>"+
                            "<ns2:model>Titan</ns2:model>"+
                            "<ns2:modelNumber>model number</ns2:modelNumber>"+
                            "<ns2:vehicleEngineCode>engine code</ns2:vehicleEngineCode>"+
                            "<ns2:odometerReading>75013</ns2:odometerReading>"+
                            "<ns2:vehicleProductionDate></ns2:vehicleProductionDate>"+
                            "<ns2:bodyStyleCode>Body style code</ns2:bodyStyleCode>"+
                            "<ns2:bodyStyle>XYZ</ns2:bodyStyle>"+
                            "<ns2:cccVehicleId>XYZ001</ns2:cccVehicleId>"+
                        "</ns5:vehicleInfo>"+
                        "<ns5:requiredDeliveryDate>2013-05-04T15:26:35.219-06:00</ns5:requiredDeliveryDate>"+
                        "<ns5:comments>Delivery date important</ns5:comments>"+
                        "<ns5:createdDate>2013-05-04T15:26:35.219-06:00</ns5:createdDate>"+
                    "</ns5:PurchaseOrder>"+
                "</ns7:NewPORequest>"+
            "</S:Body>"+
        "</S:Envelope>";
try {
JSONObject xmlJSONObj = XML.toJSONObject(TEST_XML_STRING);
Object header=xmlJSONObj.get("S:Envelope");
JSONObject jsonObject = start.getJSONObject(0);
JSONArray dependencies = jsonObject.getJSONArray("list");
JSONArray dependencies = jsonObject.getJSONArray("getData");
String data = dependencies.getString(0);
System.out.println(data);
} catch (JSONException je) {
System.out.println(je.toString());
}

I have used JAXB is past but as this is for our Automation scripts and i want see if JSON can be used here, so that i don't have any dependencies on WSDL. "Object header=xmlJSONObj.get("S:Envelope");" does provide me header and other details but if i need vehicleInfo i will have to create object for all other parent tags.

Akshay
  • 657
  • 2
  • 19
  • 38

1 Answers1

0

I am able to resolve issue, it was issue with JSONObject, i was trying to access child node in below sequence.

test=xmlJSONObj.getJSONObject("S:Envelope").getJSONObject("S:Header").getJSONObject("").getJSONObject("ns7:NewPORequest").getString("ns7:tracingLevel");

Problem here is that when SOAP Messages gets converted into JSON, the header doesn't have any value, by removing header from my string i am able to get node values.

String test=xmlJSONObj.getJSONObject("S:Envelope").getJSONObject("S:Body").getJSONObject("ns7:NewPORequest").getString("ns7:userId");
Akshay
  • 657
  • 2
  • 19
  • 38