Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python lxml getpath error

Tags:

python

lxml

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

like image 947
kingwiiwii Avatar asked Sep 03 '25 03:09

kingwiiwii


1 Answers

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)
like image 55
Padraic Cunningham Avatar answered Sep 04 '25 23:09

Padraic Cunningham