1

I'm trying to convert a JSON string to XML using jsonlib in Java.

    JSONObject json = JSONObject.fromObject(jsonString); 
    XMLSerializer serializer = new XMLSerializer();
    String xml = serializer.write( json );  
    System.out.println(xml);

The error that I get is

    nu.xom.IllegalNameException: 0x24 is not a legal NCName character

The problem here is that I have some properties in my JSON that are invalid XML characters. eg. I have a property named "$t". The XMLSerializer throws the exception while trying to create a XML tag in this name because $ is not allowed in XML tag names. Is there any way in which I can override this XML well formedness check done by the serializer?

Jophin Joseph
  • 2,864
  • 4
  • 27
  • 40
  • why would you want to override that check (and generate invalid xml)? – jtahlborn Oct 28 '11 at 12:08
  • I'm trying to use the Google REST API. After firing the webservice, I get a JSON response. I have a tree viewer that displays XML in the tree structure. So I have to convert JSON to XML to display it as a tree. But in JSON "$" is a valid property name. Google API returns property names like "$t", "gCal$timesCleaned" etc. So I'm getting an error when converting it to XML. – Jophin Joseph Oct 28 '11 at 12:29

3 Answers3

0

First I'd suggest to add the language you are using (it is Java, right?).

You could override the method where it checks your XML tag name to do nothing.

synner
  • 105
  • 7
0

I took a look at the spec for the json-lib XMLSerializer and to my surprise it seems to have no option for serialising a JSON object whose keys are not valid XML names. If that's the case then I think you will need to find a different library.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
0

You could loop over json.keySet (recursively if necessary) and replace invalid keys with valid ones (using remove and add).

RoToRa
  • 37,635
  • 12
  • 69
  • 105