xml parsing - XML Tree traversal in python -
i traversing xml file in python this:
for node in rootnode1.iter(): print node.tag my output is:
student int_class_id name studentid emailid address int_class_id street city postalcode seminar int_class_id seminarnumber course now want save names above int_class_id this:
student address seminar can 1 help?
my xml file this:
<?xml version='1.0' encoding='utf-8'?> <modeldiff> <student> <int_class_id>1</int_class_id> <name>a</name> <studentid>1</studentid> <emailid>br</emailid> <address> <int_class_id>3</int_class_id> <street>c</street> <city>p</city> <postalcode>d</postalcode> </address> <seminar> <int_class_id>4</int_class_id> <seminarnumber>e</seminarnumber> <course type="f"> <int_class_id>11</int_class_id> <topicname>g</topicname> <day>monday</day> <date>15/04/2013</date> </course> </student>
using xml.etree !
import xml.etree.elementtree et tree = et.parse('country_data.xml') root = tree.getroot() print children of root with
>>> child in root: ... print child.tag, child.attrib with input xml file
<?xml version="1.0"?> <data> <country name="liechtenstein"> <rank>1</rank> <year>2008</year> <gdppc>141100</gdppc> <neighbor name="austria" direction="e"/> <neighbor name="switzerland" direction="w"/> </country> <country name="singapore"> <rank>4</rank> <year>2011</year> <gdppc>59900</gdppc> <neighbor name="malaysia" direction="n"/> </country> <country name="panama"> <rank>68</rank> <year>2011</year> <gdppc>13600</gdppc> <neighbor name="costa rica" direction="w"/> <neighbor name="colombia" direction="e"/> </country> </data> command gives
>>> child in root: ... print child.tag, child.attrib ... country {'name': 'liechtenstein'} country {'name': 'singapore'} country {'name': 'panama'}
Comments
Post a Comment