2014-04-04 234 views
0

我有以下代碼。這個想法是將xml字符串轉換爲Json,將其存儲在數據庫(必須是JSON)並檢索並以原始格式顯示。問題是,我不能將JSON轉換成XML儘管以前使用相同的庫將XML轉換爲JSON將xml轉換爲json並返回到xml返回錯誤

我的代碼

String xml = "<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:typ=\"http://org.typ.com/resto\">\r\n" 
      + " <soapenv:Header/>\r\n" 
      + " <soapenv:Body>\r\n" 
      + "  <typ:MY-OPERATION>\r\n" 
      + "   <RequestCall>\r\n" 
      + "   <clientIp>0.0.0.0</clientIp>\r\n" 
      + "   <data xsi:nil=\"true\" />\r\n" 
      + "   </RequestCall>\r\n" 
      + "  </typ:MY-OPERATION>\r\n" + " </soapenv:Body>\r\n" + "</soapenv:Envelope>"; 

    XMLSerializer serializer = new XMLSerializer(); 
    JSON jsonObject = serializer.read(xml); 
    String jsonString = jsonObject.toString(); 
    System.out.println(jsonString); 
    serializer.setTypeHintsEnabled(false); 
    JSON jsonObject2 = JSONSerializer.toJSON(jsonString); 
    String xmlOut = serializer.write(jsonObject2); 
    System.out.println(xmlOut); 

輸出是JSON以下(是非常怪異的形式)

04/04/2014 17:44:50 net.sf.json.xml.XMLSerializer getType 
    INFO: Using default type string 
    {"@xmlns:soapenv":"http://schemas.xmlsoap.org/soap/envelope/","@xmlns:typ":"http://org.typ.com/resto","@xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance","soapenv:Header":null,"soapenv:Body":{"typ:MY-OPERATION":{"@xmlns:typ":"http://org.typ.com/resto","RequestCall":{"clientIp":"0.0.0.0","data":{"@xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance","@xsi:nil":"true"}}}}} 
    Exception in thread "main" nu.xom.NamespaceConflictException: Attribute prefixes must  be declared. 
     at nu.xom.Attribute._setNamespace(Unknown Source) 
     at nu.xom.Attribute.<init>(Unknown Source) 
     at nu.xom.Attribute.<init>(Unknown Source) 
     at net.sf.json.xml.XMLSerializer.processJSONObject(XMLSerializer.java:962) 
     at net.sf.json.xml.XMLSerializer.processJSONValue(XMLSerializer.java:1040) 
     at net.sf.json.xml.XMLSerializer.processJSONObject(XMLSerializer.java:990) 
     at net.sf.json.xml.XMLSerializer.processJSONValue(XMLSerializer.java:1040) 
     at net.sf.json.xml.XMLSerializer.processJSONObject(XMLSerializer.java:990) 
     at net.sf.json.xml.XMLSerializer.processJSONValue(XMLSerializer.java:1040) 
     at net.sf.json.xml.XMLSerializer.processJSONObject(XMLSerializer.java:990) 
     at net.sf.json.xml.XMLSerializer.processJSONValue(XMLSerializer.java:1040) 
     at net.sf.json.xml.XMLSerializer.processJSONObject(XMLSerializer.java:990) 
     at net.sf.json.xml.XMLSerializer.write(XMLSerializer.java:605) 
     at net.sf.json.xml.XMLSerializer.write(XMLSerializer.java:570) 
     at com.monguito.MongoService.main(MongoService.java:92) 
+0

_「是非常奇怪的JSON的形式」_這是諷刺,對嗎? –

+0

我的意思是,庫爲每個xml字段添加了對json字符串的命名空間,並引用了一個類型。如果我使用例如xml xsi:nill。 json輸出將添加完整的名稱空間。 你有沒有注意到或者你評論沒有看到任何東西? – user1655510

+0

線程「main」..._Exception中的異常是錯誤。您可能已經知道這一點,但尚不清楚。 –

回答

1

我可以建議只使用org.json.XML代替XMLSerializer?它會簡化代碼(它適用於我):

// xml to json 
    JSONObject jsonObject = XML.toJSONObject(xml); 
    String jsonString = jsonObject.toString(); 
    // json to xml 
    JSONObject jsonObject2 = new JSONObject(jsonString); 
    String xmlOut = XML.toString(jsonObject2);