3

I'm trying to get a full list of xpaths from a device config in xml.

When I run it though I get:

 AttributeError: 'Element' object has no attribute 'getpath'

Code is just a few lines

import xml.etree.ElementTree
import os
from lxml import etree

file1 = 'C:\Users\test1\Desktop\test.xml'
file1_path = file1.replace('\\','/')


e = xml.etree.ElementTree.parse(file1_path).getroot()

for entry in e.iter():
    print e.getpath(entry)

anyone come across this before ?

Thanks

Richie

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
kingwiiwii
  • 45
  • 1
  • 6
  • I'm trying to get it to dump out the full XPath of a files elements . http://stackoverflow.com/questions/1577293/how-to-get-path-of-an-element-in-lxml Seems to say it can be done. – kingwiiwii Jun 11 '16 at 23:00

1 Answers1

2

You are doing it incorrectly, don't call getroot just parse and iter using lxml.etree:

import lxml.etree as et

file1 = 'C:/Users/test1/Desktop/test.xml'

root = et.parse(file1)
for e in root.iter():
    print root.getpath(e)

If you are dealing with namespaces you may find getelementpath usefule:

 root.getelementpath(e)
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321