Creating ordered elements in XML

Posted on Tuesday 20 May 2008

XML doesn't inherently support element ordering. So, if you have XML that looks like this (below), you can't deterministically say that the element with "foo" data comes before element with "bar" data.

<element>
   <data>foo</data>
</element>
<element>
   <data>bar</data>
</element>
<element>
   <data>monkey</data>
</element>
<element>
   <data>fish</data>
</element>

You can get around this XML limitation by explicitly adding an order/index attribute to your ordered elements. For example:

<element n="1">
   <data>foo</data>
</element>
<element n="2">
   <data>bar</data>
</element>
<element n="3">
   <data>monkey</data>
</element>
<element n="4">
   <data>fish</data>
</element>

In order to work with data like this, you'll need to pre-populate the "n" attribute before you generate the XML (iterating over the elements and setting the "n" attribute) and you'll need to post-process the "n" attribute after you parse the XML (iterating over the elements to get the "n" attribute and reordering the elements appropriately).

Here's a LabVIEW example that demonstrates this technique (note that this example uses JKI's EasyXML library):

front panel.png

block diagram.png

For more tips and tricks for using XML in LabVIEW, and EasyXML in particular, visit the EasyXML Tips and Tricks page on the JKI Software website.


No comments have been added to this post yet.

Leave a comment

(required)

(required)


Information for comment users
Line and paragraph breaks are implemented automatically. Your e-mail address is never displayed. Please consider what you're posting.

Use the buttons below to customise your comment.


RSS feed for comments on this post |

 

Bad Behavior has blocked 778 access attempts in the last 7 days.