Python XML XPath partial failure message -
in code maintain there's xml parsing using minidom library.
for xml structure similar below:
<a val="a1"> <b val="b1"> <c val="c1"> data </c> </b> </a>
code looks like:
for in doc.getelementsbytagname("a"): aid = a.getattribute("val").encode('ascii') if aid == aexpected: afound = break else: # not found raise exception("no '%s' found" % aexpected) b in afound.getelementsbytagname("b"): bid = b.getattribute("val").encode('ascii') if bid == bexpected: bfound = b break else: # not found raise exception("no b '%s' found" % bexpected) # similar c
i wanted use xpath data. can (elementtree):
root.findall(".//a[@val=%s]/b[@val=%s]/c[@val=%s]" % (aexpected, bexpected, cexpected))
the code looks nicer now. but, when no data can found in xml, findall() returns none , have manually analyze file first non-matching element.
is there possibility in elementtree (or other xml api) both use xpath , have xpath returning first point of matching failure (similarly else clauses in original code)?
as pointed in 1 answer, code may substituted with:
afound = root.find(".//a[@val=%r]" % (aexpected,)) if not afound: raise("a not present") bfound = afound.find("b[@val=%r]" % (bexpected,)) if not bfound: raise("b not present") cfound = bfound.find("c[@val=%r]" % (cexpected,)) if not cfound: raise("c not present")
yes, that's cleaner original, looking library, give info me.
for following xml
<a val="a1"> <b val="b1"> <c val="c1"> data </c> </b> </a>
works code
import xml.etree.elementtree et file = "sample.xml" aexpected = "a1" bexpected = "b1" cexpected = "c1" tree = et.parse(file) root = tree.getroot() bfound = root.find("./b[@val='" + bexpected + "']") cfound = root.find(".//c[@val='" + cexpected + "']") print(root) print(bfound) print(cfound)
output is:
<element 'a' @ 0x02919b10> <element 'b' @ 0x02919bd0> <element 'c' @ 0x02919c30>
the xml.etree.elementtree not find xpath becouse root element
if want find element modify xml in following way
<root> <a val="a1"> <b val="b1"> <c val="c1"> data </c> </b> </a> </root>
and code
import xml.etree.elementtree et file = "sample.xml" aexpected = "a1" bexpected = "b1" cexpected = "c1" tree = et.parse(file) root = tree.getroot() afound = root.find("./a[@val='" + aexpected + "']") bfound = root.find(".//b[@val='" + bexpected + "']") cfound = root.find(".//c[@val='" + cexpected + "']") print(afound) print(bfound) print(cfound)
the result be
<element 'a' @ 0x02919b10> <element 'b' @ 0x02919bd0> <element 'c' @ 0x02919c30>
best regards
Comments
Post a Comment