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

在Java中将XML转换为JSON后读取XML节点,java,xml,json,Java,Xml,Json,我正在尝试将我的XML转换为JSONObject和JSONArray,我想从JSONObject或JSONArray中检索子节点示例,请有人帮助我如何从子节点读取数据 String TEST_XML_STRING ="<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">"+ "<S:Header/>"+ "<S:Body>"+

我正在尝试将我的XML转换为JSONObject和JSONArray,我想从JSONObject或JSONArray中检索子节点示例,请有人帮助我如何从子节点读取数据

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有任何依赖性。objectheader=xmlJSONObj.getS:Envelope;确实为我提供了标题和其他详细信息,但如果我需要vehicleInfo,我必须为所有其他父标记创建对象。

我能够解决问题,这是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");

看看这篇文章:我试过了,但它没有给我节点元素,我可以将XML转换成JSON并打印出来。