tags:

views:

42

answers:

1

I have a xml file, book.xml (http://msdn.microsoft.com/en-us/library/ms762271(VS.85).aspx)

I would like to cat books.xml and get all book ids and genres for the book id.

Similar to

cat books.xml | python reader.py

Any tips or help would be appreciated. Thanks.

+5  A: 

To read an XML file from stdin, just use etree.parse. This function accepts a file object, which can be sys.stdin.

import sys
from lxml import etree

tree = etree.parse(sys.stdin)

print ( [(b.get('id'), b.findtext('genre')) for b in tree.iterfind('book')] )
KennyTM
+1. Compact and easy to read.
Manoj Govindan