AttributeError: 'ElementTree' object has no attribute 'tag' in Python

15,224

You need to parse it from the root node

import xml.etree.ElementTree as ET
from xml.etree.ElementTree import tostring
tree=ET.parse('t.xml')
tree = tree.getroot()
t = tostring(tree)
t = t.lower()
tree= ET.fromstring(t)
Share:
15,224
nina_dev
Author by

nina_dev

Updated on July 24, 2022

Comments

  • nina_dev
    nina_dev almost 2 years

    I want to parse an XML file and extract the nodes that I am interested in if the nodes contain a specific string (keyword). But to use find and finall functions, first I decided to lower case the list of the keywords that I have, as well as the XML file. Here is code.

    import xml.etree.ElementTree as ET
    from xml.etree.ElementTree import tostring
    import csv
    tree=ET.parse('/Users/m/Documents/dr.xml')
    **t = tostring(tree)**
    t = t.lower()
    tree= ET.fromstring(t).......
    

    I get error on this line:

    t = tostring(tree)
    

    Any idea how this can be fixed? Thx

  • nina_dev
    nina_dev almost 6 years
    Thanks @Madhan Varadhodiyil
  • Madhan Varadhodiyil
    Madhan Varadhodiyil almost 6 years
    No problem.@nina_dev