Friday, 15 June 2012

How to get parent of a node with some specific value in xml using lxml python -


I'm new to XML, and there is a problem parsing it. I have XML:

  & lt; Bookstore & gt; & Lt; Book & gt; & Lt; Name & gt; ABC & lt; / Name & gt; & Lt; Price & gt; 30 & lt; / Pricing & gt; & Lt; / Book & gt; & Lt; Book & gt; & Lt; Name & gt; Learning XML & lt; / Name & gt; & Lt; Price & gt; 56 & lt; / Pricing & gt; & Lt; / Book & gt; & Lt; Book & gt; & Lt; Name & gt; Learning Java & lt; / Name & gt; & Lt; Price & gt; 340 & lt; / Pricing & gt; & Lt; / Book & gt; & Lt; Book & gt; & Lt; Name & gt; Learning Python & lt; / Name & gt; & Lt; Price & gt; 560 & lt; / Pricing & gt; & Lt; / Book & gt; & Lt; / Bookstore & gt;  

I have to know the name of the book, which is worth 30.

property = "text">

You can use & lt; name & gt; / code> equal to 30:

  // Book [value = 30] / name  

Python example:

  Lxml Import Entry Tree = etree.parse ('path_to_your_xml.xml') results from = Tree.xpath ('// book [value = 30] / name') [0] print result. Text # ABC printed ABC  

No comments:

Post a Comment