Using XML Data in LabVIEW is Hard

Posted on Monday 21 January 2008

XML, which stands for e “9″>Xtensible “75″>Markup “11″>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: “53″><Person Nationality=”US”> “font-family: courier new,courier,monospace;” zid=”20″> “54″>  <Name> “font-family: courier new,courier,monospace;” zid=”21″> “55″>    <First>John</First> style=”font-family: courier new,courier,monospace;” zid=”22″> “56″>    <Last>Doe</Last> style=”font-family: courier new,courier,monospace;” zid=”23″> “57″>  </Name> “font-family: courier new,courier,monospace;” zid=”19″> “58″></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 “60″>Flatten to XML function to convert a cluster to an XML string. “” zid=”59″> Figure 2 – LabVIEW’s “94″>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: style=”font-family: courier new,courier,monospace;” zid= “92″><LabVIEW>\vi.lib\Utility\LVXMLSchema.xsd. Generally speaking the LabVIEW XML schema uses the following format: “68″><{Data Type}> “font-family: courier new,courier,monospace;” zid=”26″> “69″><Name>{Data Label}</Name> “font-family: courier new,courier,monospace;” zid=”64″> “70″><{Attribute Name}>{Attribute Value}</{Attribute Name}> “font-family: courier new,courier,monospace;” zid=”66″> “71″><Val>{Data Value}</Val> “font-family: courier new,courier,monospace;” zid=”67″> “72″></{Data Type}> One of the disadvantages of this format is that it does not allow you generate or parse “84″>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 “font-weight: bold;” zid=”78″>Flatten To XML or “font-weight: bold;” zid=”79″>Unflatten From XML functions to generate or parse that data.

Creating Arbitrary XML Data

A natural and straight-forward approach is to use the “font-weight: bold;” zid=”109″>Format into String function to create XML data when some special schema is required. “105″> vspace=”0″ zid=”111″> Figure 3 – Converting LabVIEW data to an arbitrary XML schema zid=”106″> 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 “″ target= “_blank” title=”" zid=”99″>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). “138″> 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! “” vspace=”0″ zid=”135″> Figure 4 – Parsing an XML string to extract an element’s attribute


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… ;) “163″> 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.]

    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! ;-)

    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.

    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

    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.

    May 2, 2008 | 9:37 am

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

    angelo trivelli
    September 16, 2008 | 4:29 am

    Whenever I’ve needed to process XML in LabVIEW, I have found the MSXML activeX controls to be effective. There are a bunch of them and you can perform XSLT transformations too.

    There are both DOM and SAX based parsers. DOM-based is good for the typical use-cases a labview developer would come across (if XML is relatively small– fits in memory). I haven’t used SAX-based but I bet it works fine.

    The other advantage of using MSXML is that you have some degree of portability, exactly the same properties and invocations can be used in a VBA or VB script.

    Isaac Fayad
    February 22, 2009 | 2:30 am

    Hello Jim Kring ;
    Could you please tell me where could i find the second block in Figure 4 after “xml string” block. I searched in Function Palette and i could not find any thing. I have also the the Internet Toolkit installed on my PC.

    Ton Plomp
    February 22, 2009 | 7:35 am

    Hi Isaac,
    that function is from the Internet Toolkit, sold as an addon to LabVIEW by National Instruments.
    The toolkit is part of the Professional Development Suite.


    Wajid Gulistan
    May 31, 2009 | 12:23 am

    I really liked your article and it is very suitable one, I have a question is there any method through which i can automatically transform my whole block diagram code into XML,



    May 31, 2009 | 5:42 am

    Wajid: The short answer is “no”, you can’t transform your block diagram into XML.

    November 11, 2010 | 11:12 am

    Jim, excellent article! Thanks to this site I now know what I’m getting into before getting into it.

    May 2, 2011 | 1:38 am

    Very helpful article, thx, but I’ve got one question about passing xml data through tcp interface.
    If I’m right, I can convert xml table to xml string, which can be converted to string and passed through. And after that recieved string should converted again to xml. Will it work?
    BTW, will it work if xml will be generated NOT in LabView?

Sorry, the comment form is closed at this time.

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