0

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)
3
  • "Is very weird the form of the json" That's sarcasm, right? Commented Apr 4, 2014 at 20:52
  • I mean, the library adds namespace on json string for each xml field with a reference to a type. If i use for example inside the xml xsi:nill. json output will have added the complete namespace. Did you notice that or you commented without see anything? Commented Apr 4, 2014 at 21:20
  • Exception in thread "main"... etc. is an error. You may have already know that, but it isn't clear. Commented Apr 4, 2014 at 21:25

1 Answer 1

1

Might I suggest just using org.json.XML instead of XMLSerializer? It would simplify 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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.