Variant to XML

Converting to XML format is a good way of exporting data in a standard, human-readable way. I have used it to pass and receive data from other languages, for example, or if you need to dump data to a log file.

As you may know, LabVIEW includes its own functions for converting to and from XML, called Flatten To XML and Unflatten From XML. Unfortunately, it's a raw conversion that just gives a node with the data type and two child nodes, name and value. So, to find a field you must recover first all the name nodes, locate the one you need and then find its value sibling. Too crude for a lot of scenarios:

So I developed my own library to do this conversion in a more convenient way. Check the differences in the next image:

As you can see the field names are now used as tag names, resulting in a shorter and cleaner XML text. The reverse conversion (from XML to LabVIEW data) is shown in the bottom of the image, and works the same.

In the block diagram, these functions are used directly, just transform the resulting variant to the specific data type, as shown:

Remember to be careful with field names, don't use strange characters or spaces as that can conflict with XML guides. I also added an URL encoded option, useful when transmitting data over the net.

Download Variant to XML (LLB for LabVIEW 2016)

Of course, I haven't covered all possible data types, just the ones I use, so maybe you'll need to edit my code and add your own; go ahead, it's easy. Currently the library accepts integers (all types), enums, float (single and double), string, path, waveforms, and arrays and clusters composed of those data types. Variant attributes are exported as tag attributes, a good way to include additional info on your XML.

Comentarios

Entradas populares de este blog

SNMP library for LabVIEW

Base64 Encoding

Retrieve CPU temperature