Python Forum

Full Version: Parsing xml
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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;

Output:
</outputParameters> element[0] = <Element 'folder' at 0x13de8da0> Attribute None <outputParameters> <folder automatic="1" destination="C:\Users\ataneja\ajay\miscellaneous\" extension="*" folderPath="C:\Users\ataneja\ajay\miscellaneous\" id="targetDir" type="bin" upLoadable="RightNow" /> &lt;ReportName&gt;GlobalResults&lt;/ReportName&gt; </outputParameters>
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 code

 print('element.attrib = ', element.attrib)
I get;
Output:
element.attrib = {} Attribute None
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?
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
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])