Simple XML to JSON converter script

edited October 2016 in Questions about Code

Hi,

I'd thought this may be a quick program to write in processing, but I'm stuck on the basics. I'm attempting to convert a simply structured XML file into JSON, but I cannot get the syntax to work.

My XML file structure looks as follows:

<?xml version="1.0" encoding="windows-1252" ?>
<TABLE>
   <DD>
      <ID> AB-1234567 </ID>
      <DOMAIN> DD </DOMAIN>
      <USUB> AB-0000000-0000001 </USUB>
      <DDSEQ> 1 </DDSEQ>
      <DDDTC missing=" " />
      <DDDY missing="." />
   </DD>
   <DD>
      <ID> AC-1234567 </ID>
      <DOMAIN> DD </DOMAIN>
      <USUB> AB-0000000-00000002 </USUB>
      <DDSEQ> 1 </DDSEQ>
      <DDDTC> 2015-06-03 </DDDTC>
      <DDDY> -1 </DDDY>
   </DD>
</TABLE>

and I'd like a JSON that looks like the following (taking into account the text and numerics and respective missing values):

[  
   {
    "ID": "AB-1234567",
    "DOMAIN": "DD",
    "USUB": "AB-0000000-0000001",
    "DDSEQ": 1,
    "DDDTC": "",
    "DDDY": .,
  },
  {
    "ID": "AB-1234567",
    "DOMAIN": "DD",
    "USUB": "AB-0000000-0000002",
    "DDSEQ": 1,
    "DDDTC": "2015-06-03",
    "DDDY": -1,
  }
]

My fairly poor programming attempt so far is as follows:

XML xml;

void setup() {
  xml = loadXML("dd.xml");
  XML[] children = xml.getChildren("DD");
  println(xml.getName());
  for (int i = 0; i < children.length; i++) {
    println(i + " "+ children[i].getChild(i) + " "+ children[i].getName());
  }
}

I don't get what I expected when I list the children as I would have expected 6 children, but I only get 1.

Can anyone point me in the right direction?

Many thanks

Answers

  • I'd expect 2 from your xml - or isn't it the entire xml-file?

  • This isn't the entire table, but it's the one I'm testing my program with.

    I'm obviously misunderstanding what's happening as I thought I would have 2 children from 'TABLE' and 6 children from 'DD'. I'd assumed that with the selection:

    XML[] children = xml.getChildren("DD");
    

    I would see 6 children in array. In other words, all the key value pairs. I still don't really understand why I'm only seeing the first one though.

  • I wasn't able to solve it.

    XML xml;
    
    void setup() {
    
      size(200, 100); 
    
      xml = loadXML("dd.xml");
      XML[] children = xml.getChildren("DD");
    
      println(xml.getName(), children.length);
    
      for (int i = 0; i < children.length; i++) {
    
        println ("------------------------");
    
        String[] a1=children[i].listChildren();
    
        println (a1.length); 
    
        for (int i2 = 0; i2 < a1.length-1; i2+=2) {
          evalu( children[i], a1[i2], a1[i2+1]);
        } 
    
        println(a1);
        printArray(a1);
        println(i + ": "+ children[i].getAttributeCount() +  " "+ children[i].getName() +"." );
      }
    }
    
    void evalu(XML entity1, String type1, String name1) {
      println ( entity1.getName() + "///"); 
      if (type1.equals("#text")) {
        // using get string because type is text
        println(name1); 
        // String a1=entity1.getChildren(); 
        String result = entity1.getString(name1);
        String result2 = entity1.getName();
        String result3 = entity1.getContent();
        println("---->>>> "+result);
      }//if
      else println("unknown type");
    }
    //
    
  • OK, Thanks for looking. I'll keep trying.

  • Chrisir, you gave me a few ideas with your example and I came up with the following:

    XML xml;
    int count = 0;
    
    xml = loadXML("dd.xml");
    XML[] children = xml.getChildren("DD");
    JSONArray outJSON = new JSONArray();
    
    for (int i = 0; i < children.length; i++){
      JSONObject json = new JSONObject();
    
      String[] keys   = children[i].listChildren();
      String[] values = splitTokens(children[i].getContent());
    
      for (int j = 0;j<values.length;j++){
        json.setString(keys[(j+1)*2-1], values[j]);
      }
      outJSON.setJSONObject(count++, json);
    }
    saveJSONArray(outJSON, "dd.json");
    
    exit();
    

    I just need to work on the missing & text/numeric part. It still pretty clunky and I'm pretty sure it's not the right way to do it, but it almost works!

    Thanks again.

  • I agree, it's unconventional (and probably not recommended), but I looked JAXB a while ago and got lost in the details of trying to make it work. I'm an infrequent Java/Processing programmer, so the learning curve was a little too steep. It probably is only a couple of lines of code with Java, but it's only easy when you know what those lines are.

    With the XML structure I'm currently working with it just seemed like a simple case of parsing the data.

  • edited October 2016

    @setup -- certainly -- not trying to be critical! I'm just letting you (well, really, other future forum-goers) know that this hand-done approach is only recommended in a special, well-defined case such as yours. It won't work for any-old XML file.

Sign In or Register to comment.