From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.
We appreciate your patience as we improve our online experience.
From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.
We appreciate your patience as we improve our online experience.
01-15-2017 09:56 AM - edited 01-15-2017 10:01 AM
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! 🙂
Solved! Go to Solution.
01-15-2017 10:23 AM
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
01-15-2017 11:16 AM
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:
But I simply receive the error below:
Hopefully the attached .xml file will be of some use.
Thanks again for your help!
Charlie
01-15-2017 12:40 PM
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
01-15-2017 05:15 PM
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:
But I simply receive the error below:
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:
Ben64
01-16-2017 01:13 AM
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
01-16-2017 01:18 AM
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:
And this is the error I received:
I'm sure I'm just doing something silly, but I am struggling to see what! 🙂
Thanks,
Charlie
01-16-2017 07:04 AM
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
01-16-2017 08:14 AM
@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.
Ben64
01-16-2017 09:22 AM
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