0

I have an XML like this:

<process>
     <NAME source="hsfg" class="hshah" property="Name">
          <VALUE type="string"></VALUE>
          <VALUE type="None"></VALUE>
     </NAME>
     <number source="fdsg" class="hgdgf" property="gagfa">
          <VALUE type="string"></VALUE>
          <VALUE type="None"></VALUE>
     </number>
     <id source="ag" class="gfdg" property="fadg">
          <VALUE type="string"></VALUE>
          <VALUE type="None"></VALUE>
     </id>
</process>

I would like to add the text in here <VALUE type="string"></VALUE> I tried here but it add to all the VALUE not only in the attribute this <NAME source="hsfg" class="hshah" property="Name">

import xml.etree.ElementTree as ET

tree = ET.parse('Example.xml')
root = tree.getroot()

for elem in tree.iter('VALUE'):
    elem.text = 'new value'

tree = ET.ElementTree(root)
tree.write('newitems.xml')

My expectation output like this:

<process>
     <NAME source="hsfg" class="hshah" property="Name">
          <VALUE type="string">new value</VALUE>
          <VALUE type="None"></VALUE>
     </NAME>
     <number source="hsfg" class="hgdgf" property="gagfa">
          <VALUE type="string"></VALUE>
          <VALUE type="None"></VALUE>
     </number>
     <id source="hsfg" class="gfdg" property="fadg">
          <VALUE type="string"></VALUE>
          <VALUE type="None"></VALUE>
     </id>
</process>

anyone can help, please. really thankful :)

1 Answer 1

1

Try the below

import xml.etree.ElementTree as ET

xml = '''<process>
     <NAME source="hsfg" class="hshah" property="Name">
          <VALUE type="string"></VALUE>
          <VALUE type="None"></VALUE>
     </NAME>
     <number source="fdsg" class="hgdgf" property="gagfa">
          <VALUE type="string"></VALUE>
          <VALUE type="None"></VALUE>
     </number>
     <id source="ag" class="gfdg" property="fadg">
          <VALUE type="string"></VALUE>
          <VALUE type="None"></VALUE>
     </id>
</process>'''

root = ET.fromstring(xml)
to_change_parent = root.find('.//NAME[@source="hsfg"]')
to_change = to_change_parent.find('VALUE[@type="string"]')
to_change.text = 'new_value'
ET.dump(root)

output

<process>
     <NAME source="hsfg" class="hshah" property="Name">
          <VALUE type="string">new_value</VALUE>
          <VALUE type="None" />
     </NAME>
     <number source="fdsg" class="hgdgf" property="gagfa">
          <VALUE type="string" />
          <VALUE type="None" />
     </number>
     <id source="ag" class="gfdg" property="fadg">
          <VALUE type="string" />
          <VALUE type="None" />
     </id>
</process>
Sign up to request clarification or add additional context in comments.

3 Comments

hi @balderman Thank you. Sorry I edited my xml, the value of source are the same. Could you please help me to handle it? Thanks
So use the class which is not the same. See the code.
yeah, I did it. Thank you @balderman I posted my another question here based on your answer. Please check it out if you don't mind. Thanks stackoverflow.com/q/66666821/11076819

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.