Using XML Data in LabVIEW is Hard

Posted on Monday 21 January 2008

XML, which stands for eXtensible Markup Language, is text-based data format (or language) that is human readable and can be used to create arbitrary data structures.  It is designed to facilitate sharing structured data across many different systems.  Here is a simple example of XML data:

<Person Nationality=”US”>
  <Name>
    <First>John</First>
    <Last>Doe</Last>
  </Name>
</Person>

Figure 1 - simple XML data representing a person

You’ll notice that the data looks a lot like HTML, or Hypertext Markup Language (and in fact, XHTML is XML formatted data).  In XML a “schema” defines the set of the possible elements and the constraints on those elements (what sub-elements and attributes they can have).

LabVIEW’s XML Schema

There are several tools for dealing with XML data in LabVIEW.  First off, LabVIEW includes some VIs for Flattening and Unflattening LabVIEW data to and from XML.  Figure 2, below, shows how to use the Flatten to XML function to convert a cluster to an XML string.


Figure 2 - LabVIEW’s Flatten To XML function is used to convert any LabVIEW data to an XML string.

What you’ll notice is that LabVIEW uses a special XML schema for flattening data.  The schema that LabVIEW uses is defined by the XSD file located beneath the LabVIEW installation, here: <LabVIEW>\vi.lib\Utility\LVXMLSchema.xsd.  Generally speaking the LabVIEW XML schema uses the following format:

<{Data Type}>
<Name>{Data Label}</Name>
<{Attribute Name}>{Attribute Value}</{Attribute Name}>
<Val>{Data Value}</Val>
</{Data Type}>

One of the disadvantages of this format is that it does not allow you generate or parse arbitrary XML.  For example, the XML data shown in Figure 1, which represents a person, does not conform to the LabVIEW XML schema, so we cannot use LabVIEW’s Flatten To XML or Unflatten From XML functions to generate or parse that data.

Creating Arbitrary XML Data

A natural and straight-forward approach is to use the Format into String function to create XML data when some special schema is required.



Figure 3 - Converting LabVIEW data to an arbitrary XML schema

The downside of the technique shown in Figure 3 is that whenever we want to change the XML schema we have to change the items in the cluster and in the format string.  And, if we are changing the number of items in the XML, we have to add or remove items on the Unbundle by Name and Format into String functions — that’s a lot of work and leaves a lot of room for mistakes (a.k.a. “bugs”).

Parsing Arbitrary XML Data

Converting arbitrary XML data back into LabVIEW data is a bit more complicated than it is to convert LabVIEW data into arbitrary XML.  For this task, we really need to use an XML parser.  An XML parser analyzes an XML string and creates a document object that can be accessed using a Document Object Model, or DOM, API.  There are a variety of XML parsers available, including one from National Instruments which is bundled with the LabVIEW Internet Toolkit.  Also, the Microsoft XML Parser is available from LabVIEW via ActiveX and .NET and usually pre-installed on most Windows systems (and if it’s not, you can easily download it for free).

Figure 4, below, shows how to read an attribute of an XML element using the functions provided with the LabVIEW Internet Toolkit.  As you can see, the process is quite tedious.  And, we haven’t even extracted the person’s name, yet.  That is even more tedious!



Figure 4 - Parsing an XML string to extract an element’s attribute

Conclusion

Dealing with arbitrary XML data (that does not conform to LabVIEW’s XML schema) is generally very difficult, requiring a lot of duplicate and excessive code that is tedious and risky to maintain.  XML parsers do the work of validating and parsing the XML, but there is usually a lot of additional coding required to extract element and attribute values from the document object.   Certainly, there must be an easier way… ;)

Are you using XML in your LabVIEW applications? Are there specific XML schema that you have to use? Which aspects of working with XML data do you find difficult? What solutions have you found?

[Update: I have posted a follow-up to this article called Using XML Data in LabVIEW Just Got Easier. I hope you enjoy it.]

5 Comments for 'Using XML Data in LabVIEW is Hard'

  1.  
    akamuaka
    January 22, 2008 | 8:01 am
     

    Excellent post Jim!
    I like your method descriptions and agree with the conclusion as a whole. I wish I had had the benefit of this entry when I tackled my first app using XML in LabVIEW over 5 yrs. ago! ;-)

  2.  
    January 22, 2008 | 9:37 am
     

    akumuaka: Thanks for the feedback. I’m glad you like the article and found it useful. I’ve got some follow-up topics on XML that I think you’ll like, too. Also, I’m interested to hear about how you use XML data in LabVIEW. I’ve created a poll, here.

  3.  
    James Brunner
    January 22, 2008 | 10:01 am
     

    Nice post, Jim. I’m looking forward to the follow-up articles as well.

    One of our apps uses XML indirectly via the NI USI library. The TDM file format follows another NI XML schema entirely.

    - James

  4.  
    March 1, 2008 | 7:18 pm
     

    Hi Everyone: I wanted you all to know that I’ve posted a follow-up to this article called Using XML Data in LabVIEW Just Got Easier. I hope you enjoy it.

  5.  
    WillSmith
    May 2, 2008 | 9:37 am
     

    All that is really needed is an XSLT processor option.
    XML >- XSLT-> pretty much any kind of output.

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 |