Solved

Python XML issue

Posted on 2011-02-16
3
1,728 Views
Last Modified: 2012-05-11
I'm having difficulty parsing an xml file and then searching the tree with xpath.
What am I doing wrong here?

2 from xml.etree.ElementTree import ElementTree
    3  tree = ElementTree()
    4  myTree = tree.parse('myFile.xml')
    5  root=myTree.getroot()
    6
    7     #error1: AttributeError: _ElementInterface instance has no attribute '      > getroot'
    8
    9  for node in myTree.findall('/*/path'):
   10
   11     #error2: SyntaxError: cannot use absolute path on element
   12
0
Comment
Question by:tmonteit
  • 3
3 Comments
 
LVL 28

Accepted Solution

by:
pepr earned 500 total points
ID: 34913985
Try the following:

from xml.etree.ElementTree import ElementTree

tree = ElementTree()
myRoot = tree.parse('myFile.xml')
print myRoot

root = tree.getroot()
print root

for node in tree.findall('/*/path'):
    print node

Open in new window


The problem is that the parse() method of the ElementTree class instance does not return the ElementTree object. It returns the root element of the tree.  The .getroot() may not be applied here.  But you can apply it to the tree object.  The same holds for the .findall().

It is probably more usual to call the parse() function of the whole module (i.e. not the same-name method of the element).  Try the modified code that should do the same:

import xml.etree.ElementTree as ET

tree = ET.parse('myFile.xml')
root = tree.getroot()
print root

for node in tree.findall('./*/path'):
    print node

Open in new window


Notice that the ET is the module, not the class (a different form of the import command).
0
 
LVL 28

Expert Comment

by:pepr
ID: 34913998
I do recommend to read the Chapter 12. XML from the "Dive Into Python 3" by Mark Pilgrim.  It uses Python 3, but you can apply it also to Python 2.x.
0
 
LVL 28

Expert Comment

by:pepr
ID: 34914198
Sorry, I forgot to include the URL: http://diveintopython3.org/xml.html
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now