04-16-2019 08:56 AM
Hello everyone,
I have trouble parsing XML in LabVIEW 2017.
My XML-file looks like the following:
<?xml version="1.0" encoding="UTF-8" ?> <ResultData> <Info> <Item name="Version"> <Param name="major" key="88" value="0">major => application software version major number</Param> <Param name="minor" key="89" value="0">minor => application software version minor number</Param> </Item> <Item name="Time"> <Param name="year" key="93" value="2019">year => application software build year</Param> <Param name="month" key="94" value="4">month => application software build month</Param> </Item> </Info> </ResultData>
What I wanna do is to look for the "major" in "Version" and get the following "value".
This is what I tried so far:
I can't figure out what's the correct XPath-Command to accomplish this task.
Mabe there is some XML-expert who can help me!
Thanks,
Sylvan
04-16-2019 09:15 AM
Help us to help you. Instead of making us type the XML File example in ourselves, why don't you save us the time and effort (and encourage us to spend our time helping you) by providing the XML file, yourself?
Bob Schor
04-16-2019 09:38 AM
Hi Bob,
I thought I did, but it seems the forum doesn't allow .xml extensions. So here it is packed as .zip.
04-17-2019 04:57 AM
Not really related to LabVIEW, so not an expert, but maybe:
//.[@name="major"]/@value
04-17-2019 10:32 AM
I think the problem is 2 parts. First, I think this is the right xpath:
/ResultData/Info/Item[@name="Version"]/Param[@name="major"]
But after that, if you just use "Get XML" you still don't get just the value. You need to add more code to search for just the one attribute:
04-17-2019 10:48 AM
I found this site - https://www.w3schools.com/xml/ - extremely helpful in getting me to think about how to organize my LabVIEW xml parsing.
04-17-2019 10:56 AM - edited 04-17-2019 10:57 AM
I've also found that trying to drill down one level at a time before coding the next level helps a lot. Parse one level; if you're expecting an array of references, but you get an empty array (or the wrong size), fix it until you get the correct results. If you're expecting one reference, but you get a reference number of 0, you know you didn't parse correctly. Go and fix it and then code the next level.
Oh, and to keep open references from piling up, always close them explicitly. It's not a big deal when doing the above procedure because (hopefully) you'll never leave enough references open to cause a significant dip in resources, but you should always clean up after yourself.
04-17-2019 11:46 AM - edited 04-17-2019 11:48 AM
I'd test outside LabVIEW, for instance with https://www.freeformatter.com/xpath-tester.html.
That was simply the first google hit for "online xml xpath".
Or a online xpath generator.
04-17-2019 02:58 PM
I think LabVIEW comes with an xpath example.