Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parsing xml
#1
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?
Reply
#2
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
Reply
#3
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])
ibreeden likes this post
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020