Ir al contenido principal

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.


Entradas populares de este blog

CIDR to netmask

To begin with, something basic but useful, a LabVIEW VI for converting CIDR suffix to a standard IP mask. As you know, in CIDR format the number after the IP indicates the bits that address uses. If you need to convert to a traditional subnet mask, this VI comes handy.So, a CIDR of 24 will return a mask, 28 is, etc. Pretty simple, but a lot of applications demand netmasks in this format.Download (VI for LabVIEW 2016)LabVIEW still only supports IPv4, so I haven't delved into the complexities of IPv6 (we'd need a U128 data type for that).


Welcome to Solar Code, a personal blog by Aitor Solar (yeah, the blog's name is just a bad pun on my surname). Here I will post my programming tips and solutions, mainly for LabVIEW (though some JavaScript is foreseen). All the code and tips in this blog come with no warranty, you use it at your own risk. Any questions regarding it should be directed to Aitor Solar at