python - XML parser that contains debug information -



python - XML parser that contains debug information -

i'm looking xml parser python includes debug info in each node, instance line number , column node began. ideally, parser compatible xml.etree.elementtree.xmlparser, i.e., 1 can pass xml.etree.elementtree.parse.

i know these parsers don't produce elements, i'm not sure how work, seems such useful thing, i'll surprised if no-body has one. syntax errors in xml 1 thing, semantic errors in resulting construction can hard debug if can't point specific location in source file/string.

point element xpath (lxml - getpath)

lxml offers finding xpath element in document.

having test document:

>>> lxml import etree >>> xmlstr = """<root><rec id="01"><subrec>a</subrec><subrec>b</subrec></rec> ... <rec id="02"><para>graph</para></rec> ... </root>""" ... >>> doc = etree.fromstring(xmlstr) >>> doc <element root @ 0x7f61040fd5f0>

we pick element <para>graph</para>:

>>> para = doc.xpath("//para")[0] >>> para <element para @ 0x7f61040fd488>

xpath has meaning, if have clear context, in case root of xml document:

>>> root = doc.getroottree() >>> root <lxml.etree._elementtree @ 0x7f610410f758>

and can ask, xpath leads root element of our interest:

>>> root.getpath(para) '/root/rec[2]/para'

python xml xml-parsing

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -