0

I am new to xml, and have problem in parsing it. I have following xml:

<bookstore>
  <book>
    <name>abc</name>
    <price>30</price>
  </book>
  <book>
     <name>Learning XML</name>
     <price>56</price>
  </book>
  <book>
     <name>Learning Java</name>
     <price>340</price>
  </book>
  <book>
     <name>Learning Python</name>
     <price>560</price>
  </book>
</bookstore> 

I want to get the name of book whose price is 30. How to do that using lml python

1 Answer 1

1

You can use following XPath to select <name> element of <book> whose <price> equals 30 :

//book[price=30]/name

Python example :

from lxml import etree
tree = etree.parse('path_to_your_xml.xml')
result = tree.xpath('//book[price=30]/name')[0]
print result.text
#above printed abc
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.