Find and replacing text in elementtree
Asked Answered
G

2

3

i am very new to programming and python. I am trying to find and replace a text in an xml file. Here is my xml file

<?xml version="1.0" encoding="UTF-8"?>
<!--Arbortext, Inc., 1988-2008, v.4002-->
<!DOCTYPE doc PUBLIC "-//MYCOMPANY//DTD XSEIF 1/FAD 110 05 R5//EN"
 "XSEIF_R5.dtd">
<doc version="XSEIF R5"
xmlns="urn:x-mycompany:r2:reg-doc:1551-fad.110.05:en:*">
<meta-data></meta-data>
<front></front> 
<body>
<chl1><title xml:id="id_881i">Installation</title>
<p>To install SDK, perform the tasks mentioned in the following
table.</p>
<p><input>ln -s /sim/<var>user_id</var>/.VirtualBox $home/.VirtualBox</input
></p>
</chl1>
</body>
</doc>
 <?Pub *0000021917 0?>

I need to replace all entries of "virtual box" with "Xen". For this i tried Elementtree. But i dont know how to replace and write back to the file. Here is my try.

import xml.etree.ElementTree as ET
tree=ET.parse('C:/My_location/1_1531-CRA 119     1364_2.xml')
doc=tree.getroot()
iterator=doc.getiterator()
 for body in iterator:
    old_text=body.replace("Virtualbox", "Xen")

The texts are available in many sub tags under body.I got the method to remove the subelement and append a new element, but didnt get to replace only the texts.

Gebler answered 29/7, 2013 at 8:30 Comment(0)
S
2

Replace text, tail attributes.

import lxml.etree as ET

with open('1.xml', 'rb+') as f:
    tree = ET.parse(f)
    root = tree.getroot()
    for elem in root.getiterator():
        if elem.text:
            elem.text = elem.text.replace('VirtualBox', 'Xen')
        if elem.tail:
            elem.tail = elem.tail.replace('VirtualBox', 'Xen')

    f.seek(0)
    f.write(ET.tostring(tree, encoding='UTF-8', xml_declaration=True))
    f.truncate()
Sapsucker answered 29/7, 2013 at 9:6 Comment(2)
Hi, thanks for answer. But it neither throw any error nor replaces in the original file. I m confused what happened. Even it happened same for another script. Here it is. import xml.etree.ElementTree as ET from xml.etree.ElementTree import Element tree=ET.parse('C:/Users/epeeham/Desktop/Official/SSR-SIM/Test/1_1531-CRA 119 1364_2.xml') doc=tree.getroot() elem=Element("approved-by") elem.attrib["approved"] = "yes" i tried setting an attribute value from no to yes. But it didnt throw me an error and also didnt work. Is it a problem with my xml?Gebler
@ShahulHameed, I updated the code. The updated code use external library lxml.Sapsucker
M
1

Probably the simplest way is to do:

ifile = open('input_file','r')
ofile = open('output_file','w')
for line in ifile.readlines():
  ofile.write(line.replace('VirtualBox','Xen'))
ifile.close()
ofile.close()
Marjana answered 29/7, 2013 at 9:21 Comment(2)
Hi, thanks for your answer. I tried this way before the Eleementtree way. But my xml file was fully wiped out. The content becomes empty. dont know yGebler
It must work if you have correct permissions to read the input file and write to an output file. ifile must be different from ofile.Marjana

© 2022 - 2024 — McMap. All rights reserved.