<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.]
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!
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.
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
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.
All that is really needed is an XSLT processor option.
XML >- XSLT-> pretty much any kind of output.
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.
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.
thanks
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.
Ton
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,
Thanks
Wajid
Wajid: The short answer is “no”, you can’t transform your block diagram into XML.