Python Forum
Python to update XML SubElement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python to update XML SubElement
#1
Hi all,

I am looking for this help everywhere in the past days without success and I hope someone here can share some knowledge.
In the XML data below, how do I do for:
1 - Update the {id="012345"/res name="attrib1"/last_seem key} 'date' and 'status' attributes?
2 - Delete the {id="012345"/res name="attrib2}
3 - Add a new {id="123456"/res name="attrib2"} with 'first_seem' and 'last_seem' keys and values

Important:
- I am going to have hundred different users/IDs
- I am going to have dozens res/attributes

<stop>
<user id="012345">
<res name="attrib1">
<first_seem date="2018-07-31" status="REQUESTED">1</first_seem>
<last_seem date="2018-07-31" status="INPROCESS">0</last_seem>
</res>
<res name="attrib2">
<first_seem date="2018-07-31" status="REQUESTED">1</first_seem>
<last_seem date="2018-07-31" status="COMPLETED">0</last_seem>
</res>
</user>
<user id="123456">
<res name="attrib1">
<first_seem date="2018-07-31" status="REQUESTED">1</first_seem>
<last_seem date="2018-07-31" status="REQUESTED">0</last_seem>
</res>
</user>
</stop>

So far I found help to update values in a fixed path (i.e: /root/user/res/lastseem.text), what did not help me.
I tried to use the same command used to create the XML above changing a single/desired value, but instead of update, Python created a new entry Sad

Thank you so much for any help.
Reply
#2
you need to learn how to use lxml (XML parser) see: https://www.blog.pythonlibrary.org/2010/...with-lxml/
Reply
#3
Thank you Larz60+

Actually I am learning how to parse (read) XML records, what I am not finding anywhere (nowhere), including in the link you shared is 'How to update subelements attributes'.
Reply
#4
see: https://docs.python.org/3.7/library/xml....lement.set
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is it possible to update a CSS file from Python bigAL_python 4 2,197 Apr-20-2022, 01:38 PM
Last Post: bigAL_python
Question Python + Google Sheet | Best way to update specific cells in a single Update()? Vokofe 1 2,627 Dec-16-2020, 05:26 AM
Last Post: Vokofe
  Update Python Files Harshil 3 2,229 Aug-28-2020, 10:52 AM
Last Post: Harshil
  XML update using python sathiyarajmca 1 2,055 Jun-18-2019, 06:11 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020