?
Solved

Python XML issue

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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 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…
Suggested Courses

762 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