0

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. 
3
  • can you include your python code? Commented Feb 16, 2017 at 4:09
  • Code please! You want an attribute value, not text. Perhaps member.find('author').attrib['Value'] will give you what you want. Commented Feb 16, 2017 at 4:18
  • Code should include the module you are using to parse the XML. They have different ways of addressing attributes. Commented Feb 16, 2017 at 4:20

1 Answer 1

1

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
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.