Parsing xml - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Parsing xml (/thread-33208.html) |
Parsing xml - ateestructural - Apr-06-2021 I have the following Python code: def xml_parsing(file_name, tag_name, source_string_list, target_string_list): from xml.etree import ElementTree with open(file_name, 'rb+') as f: tree = ElementTree.parse(f) root = tree.getroot() element = root.find(tag_name) ElementTree.dump(element)The output of this (variable tag_name is outputParameters) is; In the above I want to edit the value of "destination". I thought that might be an attribute but it is not, because when I give the following line in the Python codeprint('element.attrib = ', element.attrib)I get; How do I go about editing the value of destination, it is neither a text not an attribute.I shall be grateful if someone can help? RE: Parsing xml - ateestructural - Apr-06-2021 I shall be gratly indebted if someone can point me in a direction on how to edit the "destination" Whether I ask for attributes/text of the tag outputParameters it is returned as none RE: Parsing xml - ateestructural - Apr-06-2021 I got it working. I had to iterate through subelements and then edit the attribute as below for subelem in list(element): subelem.set('destination', target_string_list[0]) subelem.set('folderPath', target_string_list[1]) |