dimanche 18 mai 2014

Java - convertir xml en json et rebrousser chemin à renvoyer des erreurs xml - Stack Overflow


I have the following code. The idea is convert an xml String to Json, store it on an database (Must be JSON) and retrieve it and show in its original format. The problem is that I cant convert the json to xml despite of previously use the same library to convert the xml to json


My code


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

Output is the following (Is very weird the form of the 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)



Might I suggest just using org.json.XML instead of XMLSerializer? It would simply the code (and it works for me):


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


I have the following code. The idea is convert an xml String to Json, store it on an database (Must be JSON) and retrieve it and show in its original format. The problem is that I cant convert the json to xml despite of previously use the same library to convert the xml to json


My code


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

Output is the following (Is very weird the form of the 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)


Might I suggest just using org.json.XML instead of XMLSerializer? It would simply the code (and it works for me):


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

0 commentaires:

Enregistrer un commentaire