Solved

Python XML issue

Posted on 2011-02-16
3
1,710 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Extract data from xml in c# 8 41
surrogate key in database world 6 72
Scraping specific data within an XML document 19 44
Create XML 5 30
Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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 …

708 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

12 Experts available now in Live!

Get 1:1 Help Now