0

I have a XML file "definitions.xml" with below content.

<Definitions>
  <Process name="en">
    <property name="am">OLD_A</property>
    <property name="24hours" xsi:type="xsd:boolean">OLD_B</property>
  </Process>
</Definitions>

I want to modify the file as below:

<Definitions>
  <Process name="en">
    <property name="am">NEW_A</property>
    <property name="24hours" xsi:type="xsd:boolean">NEW_B</property>
  </Process>
</Definitions>

I have tried below code:

from lxml import etree

def Definations_Parser():
 global Definations_tree
 global Definations_root
 parser =  etree.XMLParser(remove_blank_text = True)
 Definations_tree = etree.parse('C:\\Users\\dell\\Desktop\\definitions.xml', parser)
 Definations_root = Definations_tree.getroot()

def Definations_File_Modify():
    Process_1 = Definations_root.find('Process')
    property_1 = Process_1.find('property[@name="am"]')
    print ('Current value is:', property_1.get('name'))

def Definations_File_Write():
 Definations_tree.write('C:\\Users\\dell\\Desktop\\definitions.xml', pretty_print = True)

Definations_Parser()
Definations_File_Modify()
Definations_File_Write()

How can I get current present values "OLD_A" and "OLD_B" and change it to "NEW_A" and "NEW_B" ?

3
  • I think that you need to create a new element node which needs to replace the original one. See also here for example: stackoverflow.com/questions/22493724/… Commented Apr 7, 2018 at 19:52
  • Thanks tangoal for the response however I think I have a different situation than the link you mentioned.Is it possible for you to add in the code ? Commented Apr 8, 2018 at 6:19
  • I think you just can do property_1.text = "NEW_A" to change its text content. This is indicated in stackoverflow.com/questions/36570460/… Commented Apr 8, 2018 at 10:12

1 Answer 1

0

Like @tangoal mentioned in the comments, you use the text property of the element like so:

from lxml import etree

def Definations_Parser():
    global Definations_tree
    global Definations_root
    parser =  etree.XMLParser(remove_blank_text = True)
    Definations_tree = etree.parse('C:\\Users\\dell\\Desktop\\definitions.xml', parser)
    Definations_root = Definations_tree.getroot()

def Definations_File_Modify():
    Process_1 = Definations_root.find('Process')
    property_1 = Process_1.find('property[@name="am"]')
    print(property_1.text)
    property_1.text = 'NEW_A'
    print(property_1.text)

    property_2 = Process_1.find('property[@name="24hours"]')
    print(property_2.text)
    property_2.text = 'NEW_B'
    print(property_2.text)

def Definations_File_Write():
    Definations_tree.write('C:\\Users\\dell\\Desktop\\definitions.xml', pretty_print = True)

Definations_Parser()
Definations_File_Modify()
Definations_File_Write()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.