Solved

Python XML issue

Posted on 2011-02-16
3
1,801 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
3 Comments
 
LVL 29

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 29

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 29

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

Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
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 if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

726 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