3

I'm using net.sf.json.xml.XMLSerializer to convert XML documents to JSON. I'm getting two different results for almost two similar XML documents. My code is:

public static void main(String[] args) throws DocumentException {
    String t1="<A><B>aa</B><C><D>Martin Ritt</D> </C></A>";
    String t2="<A><B>aa</B><C><D>Martin Ritt</D></C></A>";
    System.out.println(new XMLSerializer().read(t1).toString());
    System.out.println(new XMLSerializer().read(t2).toString());
}

The first string t1 is converted to:

{"B":"aa","C":["Martin Ritt"]}

while t2 is converted to:

{"B":"aa","C":{"D":"Martin Ritt"}}

That means, in the first case, C is considered an array while in the second case, it is considered an object. The difference between the two XMLs is the space after the closing of the D element. That is, after </D>.

Any idea what is going on here? what is the rule? I'm more interested in how to make it recognize arrays consistently.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Klaus
  • 2,328
  • 5
  • 41
  • 62

1 Answers1

0

The rules are:

  • Root nodes are discarded, in this case <A>
  • Element nodes with text node children =>

    {"Element":"text node"}
    
  • Element nodes with element children =>

    {"Element":{"Element":"text node"} } //t1
    
  • Element nodes with mixed children (element + white space) =>

    {"Element":["text node"]} //t2
    

References

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265