I want to read the value of author and title from the below XML using python
<book id="bk101">
<author Value="J.K.Rowling" />
<title Value="Harry Potter"/>
</book>
Code:
member.find('author').text
# returns None.
Making some assumptions on the XML library you're using, here's an example using xml.dom.minidom:
from xml.dom import minidom
xml_string = """<book id="bk101">
<author Value="J.K.Rowling" />
<title Value="Harry Potter"/>
</book>"""
# Parse
root = minidom.parseString(xml_string)
author_list = root.getElementsByTagName("author")
for author in author_list:
value = author.getAttribute("Value")
print("Found an author with value of {0}".format(value))
Output:
Found an author with value of J.K.Rowling
member.find('author').attrib['Value']will give you what you want.