Anybody an XML expert?

Community Forums/General Help/Anybody an XML expert?

GfK(Posted 2013) [#1]
I'm not very well just now, but still soldiering on. I should know this but I'm having epic brainfarts and I just cannot think.

<?xml version="1.0" encoding="utf-8"?>

<levdata>
	<event id="1" location="32">
		<newevents>
			<event>2</event>
			<event>3</event>
		</newevents>
	</event>
	<event id="2">
	</event>
	<event id="3">
	</event>
</levdata>


Is there a way I could optimise newevents part of the above, i.e. to avoid having multiple event tags (there will often be more than two)?


Yasha(Posted 2013) [#2]
I could be wrong but I don't think XML has any kind of direct array syntax support. So the above would be the correct way to do it while sticking to pure XML.

In your position (unless I've misunderstood the issue) I would probably choose to represent newevents as a single string value, with the numbers separated by spaces or commas or whatever; this would work as long as the list only holds numeric IDs.... but also totally defeats the point of using XML though by introducing a program-specific subformat.

I think unfortunately putting up with this sort of longwindedness is one of the prices you pay for the portability/reliability/etc. of XML.

If you haven't written very much yet (or if you have, but can automate the process), it might be worth considering switching to e.g. JSON if there's going to be a lot of this.


GfK(Posted 2013) [#3]
Oh, so at least I'm not going completely mad in thinking there should be a better way.

Thanks for the info.


D4NM4N(Posted 2013) [#4]
it might be worth considering switching to e.g. JSON if there's going to be a lot of this.

Agreed, if not too far along, I would use JSON instead of XML it had a massively lower "unnesecary crap" overhead:
{
"levdata": 
 {
    "event": 
    [
         {
            "-id": "1",
            "-location": "32",
             "newevents": 
              {
                 "event": 
                 [
                      "2",
                      "3"
                 ]
              }
         },
         { "-id": "2" },
         { "-id": "3" }
     ]
  }
}

{} is an object
[] is an array of objects, strings, ints etc.....


xlsior(Posted 2013) [#5]
Depending on what you need it for: XML is much more 'human readable', so if there's any chance that the end user may need to manually modify things XML may be preferable, albeit not the most efficient format for the computer itself to deal with.


GfK(Posted 2013) [#6]
Yeah I think I'll stick with XML for three reasons. Firstly I have quite a lot of external data in XML files already. Second, I'm fairly familiar with writing XML (brainfarts aside), and finally, XML is more readable (to me) than JSON is.

All of my XML stuff is parsed at load time and dumped into various data structures, so it's all a one shot deal and speed/efficiency isn't much of a worry.