Chances are, you've probably heard of XML. If you use XML, you'll find this section very enlightening as to how LabVIEW can describe its data in XML. If you have never used XML, read on anyway, as you might find this helpful in the future. XML, short for Extensible Markup Language, is a set of rules for formatting data as text. The XML VIs (found on the Programming>>String>>XML palette) allow you to work with XML data and files (see Figure 16.9). Figure 16.9. XML palette
XML was created for the purpose of allowing data to be shared across systems in a format that is independent of both programming language and platform. Figure 16.10 shows an example of how we can use the Flatten To XML function to convert a DBL numeric to an XML string. Figure 16.10. Flatten To XML used to convert a LabVIEW numeric data into an XML representationAs we can, see our DBL numeric is converted into the following text: <DBL> <Name>Pi</Name> <Val>3.14159</Val> </DBL> The characters <DBL> signify the beginning of a data entity named "DBL" and the characters </DBL> signify its end. Everything in between is the data of the "DBL" entity. The "DBL" entity contains two sub-entities: "Name" (the data name) and "Val" (the data value). As you can see, the entities "Name" and "Value" do not have sub-entities; but rather, they have text that specifies the data values of the "Name" and "Value" entities. We can convert our XML string back into LabVIEW data using the Unflatten From XML function, as shown in Figure 16.11. If either the format or structure of the XML data is not correct, Unflatten From Error will output an error. Figure 16.11. Unflatten From XML used to convert an XML representation of a numeric into a LabVIEW numericNow, let's look at the flattened XML data of a more complex LabVIEW data structure: a cluster with two elements (see Figure 16.12). Figure 16.12. A LabVIEW cluster (left) along side its XML representation (right)
As you can see, our <Cluster> has four sub-entities: <Name>, <NumElts>, <Boolean>, and <DBL>. Notice also that <Boolean> and <DBL> have sub-entities themselves. It might be a little difficult for you to see the nesting of the entities within the XML string that is output by Flatten To String. To make discerning the relationships easier, we can apply indentation to each entity based on its level of nesting: <Cluster> <Name>Program Settings</Name> <NumElts>2</NumElts> <Boolean> <Name>Enable Controller</Name> <Val>1</Val> </Boolean> <DBL> <Name>Proportional Feedback</Name> <Val>5.40000</Val> </DBL> </Cluster> Applying indentation does not change the XML data, or entity relationships. In fact, XML ignores whitespace (tabs, spaces, carriage returns, line feeds, etc.) in between entities. However, XML does not ignore white space in an entity that is used for storing text data, such as the <Name> and <Value> entities. LabVIEW Data XML SchemaXML does not specify anything about clusters, Booleans, DBLs, or any of LabVIEW's data types. However, XML does specify the rules for parsing entity tags and sub-entities. In order to validate that an XML data structure is LabVIEW data, we need to validate it against a set of rules, called a schema, that define the allowed types of (and relationships between) entities, sub-entities, and textual data. The LabVIEW XML schema is stored in the following file: vi.lib\Utility\LVXMLSchema.xsd The file extension .xsd stands for XML Schema Definition, and the file contains an XML schema. This file is itself an XML file that conforms to an XML schemarecursion and self-consistency in a model is very, very cool! We aren't going to go into the details of the XML Schema Definition format here, but you should remember where the LabVIEW XML schema file (LVXMLSchema.xsd) is located. If you are sending LabVIEW XML data to someone, they will probably need it in order to validate the integrity of the data (especially if they are not using LabVIEW). XML ParserIf XML data conforms to the LabVIEW XML schema (for example, if it was created using the Flatten To XML function), then you can use the Unflatten From XML function to convert the data back into LabVIEW data (shown in Figure 16.13). Figure 16.13. Using Unflatten From XML to parse XML data that conforms to the LabVIEW XML schemaThe Flatten To XML function represents floating point numerics using the %.6 format (up to six decimal places). If you need to store floating point numeric data with more digits of precision, then you should use the Flatten To XML VI that is provided in the OpenG Toolkitit allows you to specify any format string that you wish. Additionally, LabVIEW's Flatten To String and Unflatten From String functions do not handle variant data; but the OpenG Toolkit Flatten To XML and Unflatten From XML VIs also overcome this limitation. See Appendix C, "Open Source Tools for LabVIEW: OpenG," for more information. There are other XML parsers that you can call from LabVIEW, which allow parsing data of any XML schema and validating the data against that schema. You can use the Microsoft XML Parser, which is accessible via ActiveX and .NET. You can also use the XML VIs offered in the Internet Toolkit from National Instruments. Another option is an open source tool called LabXML, which is available from http://labxml.sourceforge.net/. |