我正在尝试将我的XML转换为JSONObject和JSONArray,我希望从JSONObject或JSONArray中检索子节点(示例<ns2:make>
),有人可以帮助我如何从子节点读取数据。
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());
}
我已经使用了JAXB,但是因为这是我们的自动化脚本,我想看看是否可以在这里使用JSON,所以我没有任何WSDL依赖。 "Object header=xmlJSONObj.get("S:Envelope");"
确实为我提供了标题和其他细节,但如果我需要vehicleInfo,我将不得不为所有其他父标签创建对象。
答案 0 :(得分:0)
我能够解决问题,这是JSONObject的问题,我试图以下面的顺序访问子节点。
test=xmlJSONObj.getJSONObject("S:Envelope").getJSONObject("S:Header").getJSONObject("").getJSONObject("ns7:NewPORequest").getString("ns7:tracingLevel");
问题在于,当SOAP消息转换为JSON时,标头没有任何值,通过从我的字符串中删除标头,我能够获得节点值。
String test=xmlJSONObj.getJSONObject("S:Envelope").getJSONObject("S:Body").getJSONObject("ns7:NewPORequest").getString("ns7:userId");