We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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:
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.
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:
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.
Interesting approach, @setup.
If you want to do something that will convert XML to JSON in general, you can also re-use one of the many Java libraries that are for that purpose.
Some options:
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.
@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.