LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how to extract data from an arbitrary xml file and export in a nice csv file?

Solved!
Go to solution
Hallo,
I'm facing big problems in the use of XML files. I have an application which generates XML files with clusters containing arrays and scalars like in the example pasted below. My task is to read it and export the data in a human-friendly CSV document.
Since I don't know the actual content of the cluster, I need some kind of intelligent vi which goes through the XML file looking for arrays and other data structures in order to export them properly in the CSV format (columns with headers).
Thank you

<Cluster>
<Name></Name>
<NumElts>3</NumElts>
<Array>
<Name></Name>
<Dimsize>6</Dimsize>
<I32>
<Name></Name>
<Val>0</Val>
</I32>
<I32>
<Name></Name>
<Val>1</Val>
</I32>
<I32>
<Name></Name>
<Val>2</Val>
</I32>
<I32>
<Name></Name>
<Val>3</Val>
</I32>
<I32>
<Name></Name>
<Val>4</Val>
</I32>
<I32>
<Name></Name>
<Val>5</Val>
</I32>
</Array>
<DBL>
<Name></Name>
<Val>3.14159265358979</Val>
</DBL>
<String>
<Name></Name>
<Val>ciao</Val>
</String>
</Cluster>
0 Kudos
Message 1 of 12
(11,487 Views)

When you say you don't know the content are you referring to the structure of the cluster, or its values? That XML is using the LabVIEW schema. Thus, all you need is the Unflatten From XML function with the appropriate datatype:

Example_VI.png

If your cluster structure is unknown, you're talking about a whole different ballgame. You would need to try to define the boundaries of the problem, otherwise it's going to be a VERY complicated VI. For example, do you need to extract just the contents of the array? If so, then you just need to get the <Array> node, which is pretty simple.

 

You will also need to indicate which version of LabVIEW you're using. If you have 8.5 or earlier, then you will need to use third-party tools to read the XML files if you're using your own schema (which it sounds like you're doing). Options include LabXML (free) or JKI's Easy XML (not free). If you have LabVIEW 8.6 then you can use the XML Parser VIs and functions. Of course, you can always simply read it in as a string and simply parse it that way by looking for the node names using Match Pattern

Message 2 of 12
(11,479 Views)

thank you very much. just a quick reply, i'll post more detail tomorrow.

I use 8.6 version. The xml file i'm supposed to read contains clusters which may contain 1D or 2D arrays strings or double. the vi should be able to handle these different cases and put the datas in a csv file.

I think I can make a vi which surf the xml tree and has different subvi to extract different type of data.

what do you think about that?

how can i use the unflatten function if i don't know the type of the data i have to extract?

could you give me an example of the use of match pattern in this case?

thank you  very much

0 Kudos
Message 3 of 12
(11,477 Views)

The Unflatten From XML function requires a datatype wired to it. That was why I asked whether you didn't know the structure or the values, and were just trying to read the values from the XML file.

 

The LabVIEW XML schema is defined in the LVXMLSchema.xsd file located in the labview\vi.lib\Utility directory. Arrays are indicated using the <Array> node. The <Dimension> node indicated the size of a particular dimension, so having multiple <Dimension> nodes indicate multiple dimensions of an array. The node names that follow the <Dimension> nodes indicate the datatype (<I32>, <DBL>, <String>), so that tells you whether you have an array of integers, floats, or strings. 

 

 

0 Kudos
Message 4 of 12
(11,450 Views)

Thank you again,

I'm forwarding my vi draft with many comments and an xml file sample.

Data in cluster is stored according to the LabVIEW schema, infact it is generated by LabVIEW.

What I'm trying to do is to access the element of the cluster and read their content using the Invoke node and Property node functions. Could you give it a look, there may be something wrong, I'm not able to access cluster children.

Which funcions should I use? Could you give me an example? You may use the draft I enclosed...

Then write these data in a csv file should be the easier part.

 

BODY{font:x-small 'Verdana';margin-right:1.5em} .c{cursor:hand} .b{color:red;font-family:'Courier New';font-weight:bold;text-decoration:none} .e{margin-left:1em;text-indent:-1em;margin-right:1em} .k{margin-left:1em;text-indent:-1em;margin-right:1em} .t{color:#990000} .xt{color:#990099} .ns{color:red} .dt{color:green} .m{color:blue} .tx{font-weight:bold} .db{text-indent:0px;margin-left:1em;margin-top:0px;margin-bottom:0px;padding-left:.3em;border-left:1px solid #CCCCCC;font:small Courier} .di{font:small Courier} .d{color:blue} .pi{color:blue} .cb{text-indent:0px;margin-left:1em;margin-top:0px;margin-bottom:0px;padding-left:.3em;font:small Courier;color:#888888} .ci{font:small Courier;color:#888888} PRE{margin:0px;display:inline}

  <?xml version="1.0" encoding="iso-8859-1" ?>
 <Contents type="Data" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="XMLSection.xsd">
 <section name="beta" date="7/31/2009" time="3:43:03 PM" version="1.0">
 <Cluster>
  <Name />
  <NumElts>1</NumElts>
 <Array>
  <Name />
  <Dimsize>4</Dimsize>
 <DBL>
  <Name>number: 0 to 1</Name>
  <Val>0.93317638164326</Val>
  </DBL>
 <DBL>
  <Name>number: 0 to 1</Name>
  <Val>0.79233924020314</Val>
  </DBL>
 <DBL>
  <Name>number: 0 to 1</Name>
  <Val>0.39199947274518</Val>
  </DBL>
 <DBL>
  <Name>number: 0 to 1</Name>
  <Val>0.74817197429441</Val>
  </DBL>
  </Array>
  </Cluster>
  </section>
  </Contents>
 

 

0 Kudos
Message 5 of 12
(11,441 Views)
Solution
Accepted by DiegoDC

Rather than getting the <section> node you could simply go directly to the <Array> node since that' the one you're really interested in. Basically what it comes down to is to determine how many array elements you have, and this depends on whether you have 1 or 2 <Dimsize> nodes. The rest is just getting child nodes and the next siblings. See attached as a starting point. The attached XML file is for a 2D array (change the extension to .xml).

 

Notes on example:

  • I did not properly close references, so this is something you need to do.
  • It's limited to 1D or 2D arrays. 
  • I would suggest using a file path control to specify the input XML file and a file/folder path control to specify the location of the output file.
Download All
Message 6 of 12
(11,433 Views)

Amazing solution, you gave me more than a hint!

With the method you suggested I was able to read from a cluster any type of data and write them in a csv with the format I like.

Thank you again, your help has been very precious

0 Kudos
Message 7 of 12
(11,406 Views)

Hi All,

 

I have done one VI that will write xml file. I have added that subvi along with the xml file (Labview Ver 7.1).

 

Please change the .txt extension to .xml since i was not able to attach .xml file.

 

Now i want to read it back. Kindly tell me how can i read it.

 

/J

Download All
0 Kudos
Message 8 of 12
(10,918 Views)

You should have really created a new thread since your question doesn't have to do with extracting data from an XML file and exporting to a spreadsheet file.

 

Since you have your own schema then to read the XML file you have to do the reverse of what you're doing. You may want to consider looking into using LabXML or JKI's Easy XML. I'm not sure if the latter is available for 7.1, though. 

Message 9 of 12
(10,906 Views)

Thanks,

But I  keep receiving all the time the error in the Open File parser, why th?

See attached jpg

Thanks for any hint

0 Kudos
Message 10 of 12
(9,109 Views)