06-05-2012 05:36 PM
There are a few section in a XML file that I would like to edit through my labview program. Is there an easy way to do this?
06-05-2012 06:02 PM
Manipulating XML in LabVIEW is my least favorite thing. I have never used JKI's EasyXML so I don't know if it would work.
I typically use a regular expression for that.
06-05-2012 06:21 PM
Can you give me an example of doing this with regular expression? This is my first time messing with xml.
06-05-2012 07:20 PM - edited 06-05-2012 07:25 PM
If it is a simple operation I sometimes use something like the code below. It isn't very robust. For example it will mess up your XML if you specify a non-existant element but that can be fixed by checking if the offset past match is -1. If you have a more complex nested structure with multiple elements of the same name then it is best to use the XML parser. It's kind of a pain. You have to remember to close a bunch of references and the diagram can get large. There are examples that ship with LabVIEW and I always have to refer to them.
The next time I find that I am needing to use the LabVIEW XML parser I will probably check into EasyXML.
06-05-2012 08:22 PM
Do you read/write from/to an xml in string? If so, what node is good for such operation? Is there a risk of corrupting the file? Is a xml just a pure text file?
06-05-2012 09:00 PM
There are read and write xml VIs in LabVIEW. But, you should just be able to use read text file and it will read the whole file into a string.
06-05-2012 09:05 PM
'Load.vi' in the XML palette is polymorphic. Simply wire a string to the 'Path' input (top left) and you will get the string version.
As the old saying goes, if the solution to your XML problem is a regex, now you have four problems. (As you may know, I am a huge advocate for Regexes and the XML VIs. A big reason I am such a fan of both is that I now keep them separate). Besides, writing a good XPath expression is almost as much fun as a cool regex. I'll even break out the .NET XML functions before a regex.
As to file corruption, if you stick to the DOM and the XML VIs, it is hard to do much damage. Once you bring in the regex, all bets are off.
06-05-2012 09:07 PM - edited 06-05-2012 09:16 PM
@jyang72211 wrote:
Do you read/write from/to an xml in string? If so, what node is good for such operation? Is there a risk of corrupting the file? Is a xml just a pure text file?
Yes, an xml file is just a pure text file. It is the same thing as an html file - just a text file.
Edit: You are absolutely right Darin! But if someone is not familiar with XML and needs to do something as simple as my example above then string operations are probably fine. Doing this with markup that is more complex or subject to change is definately asking for trouble.
06-05-2012 09:31 PM
@Steve Chandler wrote:....if someone is not familiar with XML and needs to do something as simple as my example above then string operations are probably fine.
Certainly true, although it is almost always easier to reach for the Duct tape than the proper solution. I also try to convince people (ie. myself) that it is better to learn the more complex, yet robust method on a simple problem. Waiting for a complex problem to learn the complex solution is a recipe for frustration.
06-06-2012 08:22 AM
what did you mean by XPath expression? Can you give me an example? Also, can you give me an example on the .NET XML function as well?