Michal
Michal

Reputation: 93

python xml change tags value if they have spec child

I have 50MB xml file and i would like change specyfic tags value when they have child tag with specyfic value by python. I tried to used xml.etree.ElementTree and any stockoverflow users advices but i'm still don't have results. Can you give me some hints?

<parent>
  <child1>321</child1>
  <par_child2>
    <par_par_child1>
      <par_par__child1_child>XYZ</par_par__child1_child>
      ...
    </par_par_child1>
  </par_child2>
</parent>

And now

if <par_par__child1_child> = 'XYZ': 
  replace <child1> tag value to '123'

If in your opinion python is not the best for this task, what should I use? I will be very graceful for any help.

Upvotes: 0

Views: 43

Answers (2)

Evya
Evya

Reputation: 2375

EDIT

After your clarification, working code using lxml:

from lxml import etree


xml = """<parent>
  <child1>321</child1>
  <par_child2>
    <par_par_child1>
      <par_par__child1_child>XYZ</par_par__child1_child>
      ...
    </par_par_child1>
  </par_child2>
</parent>"""

tree = etree.fromstring(xml)
for element in tree.iter('par_child2'):
    if element.find('par_par_child1/par_par__child1_child').text == 'XYZ':
        # Traverse up and back down again, I prefer this over indexes 
        element.find('../child1').text = '123'

print(etree.tostring(tree, pretty_print=True).decode('utf-8'))

Output

<parent>
  <child1>123</child1>
  <par_child2>
    <par_par_child1>
      <par_par__child1_child>XYZ</par_par__child1_child>
      ...
    </par_par_child1>.
  </par_child2>
</parent>

Upvotes: 1

papagaga
papagaga

Reputation: 1158

for ppchild in root.iter('par_par_child1_child'):
    if ppchild.text == 'XYZ':
        ppchild.text = '123'

root is obtained by querying your tree: tree.getroot()

'XYZ' is the text of the par_par_child1_child element

Python is well suited to the task, but you have to learn it a bit before using it, even if it is easier than other languages.

Upvotes: 0

Related Questions