简体   繁体   English

在Java中将XML转换为JSON之后读取XML节点

[英]Read XML nodes after XML to JSON conversion in Java

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. 我正在尝试将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());
}

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. 我曾经使用过JAXB,但是因为这是我们的自动化脚本,所以我想看看JSON是否可以在这里使用,这样我就不会对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. 确实为我提供了标头和其他详细信息,但是如果我需要vehicleInfo,我将不得不为所有其他父标记创建对象。

I am able to resolve issue, it was issue with JSONObject, i was trying to access child node in below sequence. 我能够解决问题,这是JSONObject的问题,我试图按以下顺序访问子节点。

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. 这里的问题是,当SOAP消息转换为JSON时,标头没有任何值,通过从我的字符串中删除标头,我可以获取节点值。

String test=xmlJSONObj.getJSONObject("S:Envelope").getJSONObject("S:Body").getJSONObject("ns7:NewPORequest").getString("ns7:userId");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM