LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I edit an element's value in an XML file?

Solved!
Go to solution

Seems like I may be overcomplicating things.  I created a vi that can read an xml file that I manually created but also need to edit element values.  

 

For example i want to edit element "Count" from 4 to 5 in this xml file:

 

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<RuntimeData>
   <Extruder>
      <Date>3-2-2015</Date>
      <Count>4</Count>
      <Position>984</Position>
   </Extruder>
</RuntimeData>

 

 

Any help or direction is appriciated (besides being referred to W3Schools which was my first resort)

 

Thanks

 

Jeremy

0 Kudos
Message 1 of 9
(5,882 Views)

I believe you need to use the value property

0 Kudos
Message 2 of 9
(5,876 Views)
Solution
Accepted by topic author JeremyGabel

I really do think you need to check out the W3Schools for XPath info, makes life easy:

 

UpdateCountXML.png

Message 3 of 9
(5,863 Views)

Darin,

 

You are probably right about checking out W3Schools more but that wasnt my problem.  Since i dont use Labview on a day to day basis, i failed to remember that I can change the property of the node from "Read" to "Write" ... a big DUH!!! 

 

I knew it was something simple!

 

Thanks for your feedback!!! Much appriciated

 

Jeremy

0 Kudos
Message 4 of 9
(5,841 Views)

Hello,

 

This post is the solution to my problem but I can't seem to adapt it to my VI.

I would like to:

1-  Read the value of an element from my xml file (works)

2-  Replace by new value (doesn't work)

3 - Read the value again (works)

 

The input is an xml file that has its own scheme (xsd file). Can this be the reason the property node 'set value' isn't working?

 

The xml file and xsd file couldn't be attached. Sorry for the screen shots...

 

Thanks for any help,

 

 

PS:

 

XML content:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?><UserVars_xml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="UserVars.xsd">

  <UserVars name="495B_FT_RF">
 
  <UserVar name="RFmeasureCurrent1">
      <Default>D1</Default>
      <Type>T1</Type>
      <Measure>m1</Measure>
    </UserVar>
 
   <UserVar name="RFmeasureCurrent2">
      <Default>d2</Default>
      <Type>t2</Type>
      <Measure>m2</Measure>
 </UserVar>
 
   <UserVar name="RFmeasureCurrent3">
      <Default>d3</Default>
      <Type>T3</Type>
      <Measure>m3</Measure>
    </UserVar>

 </UserVars>
</UserVars_xml>

 

XSD CONTENT:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="UserVars_xml">
   <xs:element name="UserVars">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="UserVar" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Default" type="xs:string"/>
              <xs:element name="Type" type="xs:string"/>
              <xs:element name="Measure" type="xs:string"/>
            </xs:sequence>
   <xs:attribute name="name" type="xs:string"></xs:attribute>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
   <xs:attribute name="name" type="xs:string"></xs:attribute>
    </xs:complexType>
   </xs:element>
  </xs:element>
</xs:schema>

 

Download All
0 Kudos
Message 5 of 9
(4,561 Views)

The issue is that the text string you want to place is not the value of the node you have found, but the value of a text node (which is a child). If you look at the code for the VI "Get Node Text Content" you will see where the text comes from.  Here is a screenshot showing a way to do it, but you would need to be sure there was only one child node (you could delete all children first, and then add a new child node)

 

Message 6 of 9
(4,544 Views)

This works (the value after editing element is the right one) but the document isn't updated. Can you help me on this?

 

I guess I should be using "Replace Child Node", but I can't seem to make it work.

 

Thanks for your help and your quick answer,

0 Kudos
Message 7 of 9
(4,532 Views)

Well if you mean the xml file is not updated, you need to write it out again using the "Save" function on the XML parser pallete.  If that is not the issue, please post the code that is not working.

Message 8 of 9
(4,514 Views)

ok that works, thanks !

0 Kudos
Message 9 of 9
(4,506 Views)