LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Edit data in XML

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? 

------------------------------------------------------------------

Kudos and Accepted as Solution are welcome!
0 Kudos
Message 1 of 12
(5,833 Views)

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.

=====================
LabVIEW 2012


Message 2 of 12
(5,830 Views)

Can you give me an example of doing this with regular expression?  This is my first time messing with xml.

------------------------------------------------------------------

Kudos and Accepted as Solution are welcome!
0 Kudos
Message 3 of 12
(5,824 Views)

 

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.

 

 

Example_VI.png

=====================
LabVIEW 2012


Message 4 of 12
(5,817 Views)

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?  

------------------------------------------------------------------

Kudos and Accepted as Solution are welcome!
0 Kudos
Message 5 of 12
(5,810 Views)

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.

Message 6 of 12
(5,804 Views)

'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.

Message 7 of 12
(5,801 Views)

@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.

=====================
LabVIEW 2012


Message 8 of 12
(5,800 Views)

@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.

Message 9 of 12
(5,794 Views)

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? 

------------------------------------------------------------------

Kudos and Accepted as Solution are welcome!
0 Kudos
Message 10 of 12
(5,780 Views)