From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Read XML data

Solved!
Go to solution

Hi all,

 

I'm new to Labview so apologies in advance if this seems like a simple question, but I have a configuration XML file that i will set various parameters within to control measurement equipment.

 

I have tried reading this XML file but i'm really struggling! I need to read the contents of the individual elements, and where applicable also count the number of elements.

 

Is there an easy way that I am able to achieve this? I've been playing around with this for a few days now and I still can't get what i'm looking for.

 

I'm currently running Labview 2016 (32 bit).

 

Thanks in advance! 🙂

0 Kudos
Message 1 of 11
(13,316 Views)

It really helps us to help you if you show us what you are doing.  I assume you are trying to use some of the XML functions that come with LabVIEW -- attach your code so we can understand what you have tried.  It would also be helpful if you attached an XML file that you are trying to parse.

 

In addition to the XML functions that come with LabVIEW, there are several other XML systems compatible with LabVIEW that some of us have used.  However, we'd really need to see the XML file you are trying to read in order to suggest which you should try.

 

Bob Schor

0 Kudos
Message 2 of 11
(13,305 Views)

Hi Bob,

 

Thanks for your prompt reply - I've attached a sample copy of my XML configuration file at its current state. I've had to upload it as a text document as this forum won't allow me to upload an XML file, however I am loading it into labview as an XML file.

 

The below is one example of many of which I have currently tried:

Sample XML trials.PNG

But I simply receive the error below:

XML error.PNG

 

Hopefully the attached .xml file will be of some use.

 

Thanks again for your help!

 

Charlie

0 Kudos
Message 3 of 11
(13,280 Views)

OK, so LabVIEW reads (and parses) XML according to its own schema, as described in some of the Help documentation for the LabVIEW functions.

 

But forget that for a moment.  I looked at your XML file and tried (without worrying about Schemas or anything else) to figure out what LabVIEW structures were represented there, and how LabVIEW would be able to figure out what was what.  The root, "test", has three elements, "details", "test documents", and "measurements".  Details looks like a LabVIEW Cluster having two elements, "number" (possibly an I32) and "description", a string.  Test Documents also looks like a cluster of "number" (possibly I32) and "issue" (either a String or a Float).  Measurements is yet something else, possibly an array of clusters.

 

In order to "map" XML into LabVIEW, there needs to be an unambiguous "grammar" so that LabVIEW can figure out how to create LabVIEW elements from the text data.  You can get a look at the LabVIEW Schema by creating "known data" (perhaps whatever you think your XML data represents) and running it through Flatten to XML -- you'll notice it looks not very similar to your file.  You should also check out the Examples mentioned in the Help files.

 

Bob Schor

Message 4 of 11
(13,264 Views)

CharlieB5 a écrit :

Hi Bob,

 

Thanks for your prompt reply - I've attached a sample copy of my XML configuration file at its current state. I've had to upload it as a text document as this forum won't allow me to upload an XML file, however I am loading it into labview as an XML file.

 

The below is one example of many of which I have currently tried:

Sample XML trials.PNG

But I simply receive the error below:

XML error.PNG

 

Hopefully the attached .xml file will be of some use.

 

Thanks again for your help!

 

Charlie


First, LabVIEW cannot parse your document because of the syntax on line 18: <trace = "1">

You need to provide an attribute name, something like <trace value = "1">.

 

Second, you must first set an ID attribute to an element before you can use the Get Element by ID method.

 

Since you have more than one "number" node it would be better to use an xpath search (get familiar with this), something like this:

parse xml.png

 

Ben64

Message 5 of 11
(13,242 Views)

Many thanks for the replies guys.

 

Bob, I've now changed my .xml file to now have an identifier associated with the attributes, which I've called 'value' as you suggested.

 

Ben, that example seemed to work perfectly and was exactly what I was after. Is there an easy way of determining the number of children within a certain parent? For example, using my .xml example, if I wanted to determine the number of traces within <measurement number="1"> (in this example it should return 2) as I'm planning on looping through them when I configure the measurement equipment.

 

Many thanks again for your support!

Charlie

0 Kudos
Message 6 of 11
(13,215 Views)

Apologies for the double post, but I forgot to add what I've already tried and I couldn't find an option to edit my post. Please see below:

 

xml_attributes.PNG

And this is the error I received:

attribute error.PNG

 

I'm sure I'm just doing something silly, but I am struggling to see what! 🙂

 

Thanks,

Charlie

0 Kudos
Message 7 of 11
(13,212 Views)

Please attach code (a file that ends in .vi).  We cannot examine, edit, or execute a picture (unless, of course, it is a LabVIEW Snippet, which yours is not).

 

Bob Schor

0 Kudos
Message 8 of 11
(13,194 Views)
Solution
Accepted by topic author CharlieB5

@CharlieB5 wrote:

Many thanks for the replies guys.

 

Bob, I've now changed my .xml file to now have an identifier associated with the attributes, which I've called 'value' as you suggested.

 

Ben, that example seemed to work perfectly and was exactly what I was after. Is there an easy way of determining the number of children within a certain parent? For example, using my .xml example, if I wanted to determine the number of traces within <measurement number="1"> (in this example it should return 2) as I'm planning on looping through them when I configure the measurement equipment.

 

Many thanks again for your support!

Charlie


Instead of using Match First Node use Match All Nodes. Use the resulting array of nodes to index a FOR loop. Here is a quick example. You will still have to parse the inside part of the FOR loop. I used Get XML to show that it is getting the desired nodes.

 

xml match multiple.png

 

Ben64

Message 9 of 11
(13,186 Views)


Just quoting ben64 to add emphasis to his important advice:

 

@ben64 wrote:

... Since you have more than one "number" node it would be better to use an xpath search (get familiar with this)...

 

Ben64

My first struggles with parsing XML were *massively* eased by learning just a little basic Xpath syntax.  Xpath delivers an analogous kind of parsing magic to XML that regular expressions deliver to plaintext strings.

 

 

-Kevin P

CAUTION! New LabVIEW adopters -- it's too late for me, but you *can* save yourself. The new subscription policy for LabVIEW puts NI's hand in your wallet for the rest of your working life. Are you sure you're *that* dedicated to LabVIEW? (Summary of my reasons in this post, part of a voluminous thread of mostly complaints starting here).
Message 10 of 11
(13,177 Views)