Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Python XML issue

Posted on 2011-02-16
3
Medium Priority
?
1,938 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
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 …

610 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