#!/usr/bin/env python
"""
read an opml file

    http://www.opml.org/spec
"""

opml_sample="""\
<?xml version="1.0" encoding="UTF-8"?>
<opml version="1.0">
  <head>
    <title>fogbugz-project2</title>
    <expansionState>1,4,5,6</expansionState>
  </head>
  <body>
    <outline text="xyzzy part 1" _note="some text for 1"/>
    <outline text="xyzzy part 2">
      <outline text="xyzzy part 2a"/>
      <outline text="xyzzy part-2-b"/>
    </outline>
    <outline text="xyzzy part 3">
      <outline text="xyzzy part 3a" _note="some text for 3a">
        <outline text="xyzzy part 3a1">
          <outline text="xyzzy part 3a1A"/>
        </outline>
      </outline>
    </outline>
  </body>
</opml>
"""

##############################################################

import xml.dom.minidom

elist={}

def do_outline(dom,parent,lev=0):
    print ' '*lev*4,dom.getAttribute('text')
    if parent is not None:
        elist[parent.getAttribute('text'),dom.getAttribute('text')]=1
    else:
        elist[None,dom.getAttribute('text')]=1
    olist=dom.getElementsByTagName("outline")
    for x in olist:
        do_outline(x,dom,lev+1)

def do_body(dom):
    olist=dom.getElementsByTagName("outline")
    for x in olist:
        do_outline(x,None)

def do_opml(doc):
    dom = xml.dom.minidom.parseString(doc)
    body=dom.getElementsByTagName("body")[0]
    #body=body.getElementsByTagName("body")[0]
    do_body(body)

def main():
    do_opml(opml_sample)
    import pprint
    pprint.pprint(elist)

main()
