LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

embedding a string (in xml style) to an xml file

Hi,

 

I have created a string (in xml style by using string constants, loops, and values) and now want to embed this into an already prepared XML file under certain element without changing the schema of the XML file to LV schema. Any idea how exactly do this?

 

Rgds,

David

0 Kudos
Message 1 of 11
(4,339 Views)

Use the standard file functions - read in the string, find the place when you want to place your new string, shove it in and write it back to the file.


___________________
Try to take over the world!
0 Kudos
Message 2 of 11
(4,335 Views)

Could you elaborate more on how to write back without applying LV schema (an example)?

0 Kudos
Message 3 of 11
(4,330 Views)

You use the standard file functions, not the XML ones. I'm assuming the example finder (Help>>Find Examples) should have some examples of using those.


___________________
Try to take over the world!
Message 4 of 11
(4,324 Views)

Thanks, I managed to add the string but now the tricky part is to have certain bytes (Set File position), easy way to get that?

0 Kudos
Message 5 of 11
(4,306 Views)

What exactly do you mean by "have certain bytes (Set File position)"?

 

If you have the new string just do as tst said - just use the Write to Text File function.

Message 6 of 11
(4,300 Views)

Permit me to defend the honor of the built-in functions.  I have used regexes and the file functions to slice and dice many an XML file, but it can be quite fragile code and sometimes a bit ugly to find the right spot to insert your string.  The following snippet "loads" an XML file (it is actually a string for simplicity here) and also takes in a string representing a new chunk of XML.  I then use an XPath expression to find the right place to insert the new section, in this example the new text is inserted before the specified node, or at the end if the XPath expression is not found.  The  XML functions are a bit like the .NET functions, it looks unwieldy until you get used to them. 

 

Just modify the code to load and save the existing file, and change the XPath expression to find the insertion point.

 

InsertXMLString.png

Message 7 of 11
(4,282 Views)

I would go with the build in xml parser (not the same as labview schema vis).  These are probably more efficient and scalable then rolling your own parser.

Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
0 Kudos
Message 8 of 11
(4,278 Views)

Like the example from darin

Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
0 Kudos
Message 9 of 11
(4,277 Views)

To smercurio_fc and Darin

 

I wanted it in after a certain element in the XML file, therefore, should specify it, in case of using Write to Text file, by number of bytes. However, now I'm tryinh out the approach suggested by Darin. All the difficulties raise because I have to use a certain XML schema which different from LV schema!

 

Rgds,

David

0 Kudos
Message 10 of 11
(4,259 Views)