2013-12-18 85 views
0

我想將XML轉換爲JSONObject和JSONArray,我想從JSONObject或JSONArray中檢索子節點(例如<ns2:make>),有人可以幫助我如何從子節點。在XML中轉換XML到JSON後讀取XML節點

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

看看這個帖子:http://stackoverflow.com/questions/1823264/quickest-way-to-convert-xml-to-json-in-java –

+0

我試過了,但它不給我節點元素,我可以將XML轉換爲JSON並打印出來。 – Akshay

回答

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");