开发者

Best way to parse xml in Appengine with Python

开发者 https://www.devze.com 2023-02-02 21:20 出处:网络
I am connecting to isbndb.com for book information and their response looks like this: <?xml version=\"1.0\" encoding=\"UTF-8\"?>

I am connecting to isbndb.com for book information and their response looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<ISBNdb server_time="2005-02-25T23:03:41">
 <BookList total_results="1" page_size="10" page_number="1" shown_results="1">
  <BookData book_id="somebook" isbn="0123456789">
   <Title>Interesting Book</Title>
   <TitleLong>Interesting Book: Read it o开发者_JS百科r else..</TitleLong>
   <AuthorsText>John Doe</AuthorsText>
   <PublisherText>Acme Publishing</PublisherText>
  </BookData>
 </BookList>
</ISBNdb>

What is the best way to turn this data into an object using appengine (Python)?

I need the isbn number (a tag in BookData) but I also need the contents (as opposed to tags) of all the children of BookData.


use etree:)

>>> xml = """<?xml version="1.0" encoding="UTF-8"?>
... <ISBNdb server_time="2005-02-25T23:03:41">
...  <BookList total_results="1" page_size="10" page_number="1" shown_results="1">
...   <BookData book_id="somebook" isbn="0123456789">
...    <Title>Interesting Book</Title>
...    <TitleLong>Interesting Book: Read it or else..</TitleLong>
...    <AuthorsText>John Doe</AuthorsText>
...    <PublisherText>Acme Publishing</PublisherText>
...   </BookData>
...  </BookList>
... </ISBNdb>"""

from xml.etree import ElementTree as etree
tree = etree.fromstring(xml)

>>> for book in tree.iterfind('BookList/BookData'):
...     print 'isbn:', book.attrib['isbn']
...     for child in book.getchildren():
...             print '%s :' % child.tag, child.text
... 
isbn: 0123456789
Title : Interesting Book
TitleLong : Interesting Book: Read it or else..
AuthorsText : John Doe
PublisherText : Acme Publishing
>>> 

voila;)


There is an excellent Python module called BeautifulSoup. Use the BeautifulStoneSoup class for XML parsing.

More info: http://www.crummy.com/software/BeautifulSoup/documentation.html

0

精彩评论

暂无评论...
验证码 换一张
取 消