[Tutor] xml parsing from xml

Danny Yoo dyoo at hashcollision.org
Wed May 7 22:49:41 CEST 2014


To elaborate:

########################################################
from xml.dom.pulldom import START_ELEMENT, parse
import io

sampleData = u"""<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank updated="yes">69</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
"""


doc = parse(io.StringIO(sampleData))
for event, node in doc:
    if event == START_ELEMENT and node.localName == "country":
        doc.expandNode(node)
        print("--------------")
        print("This is the node for " + node.getAttribute('name'))
        print("--------------")
        print(node.toxml())
        print("\n\n")
########################################################


We can decide and make control-flow decisions in a stream-like manner.
 Note that we can even look at attributes, before printing the node
back wherever we want.


More information about the Tutor mailing list