from __main__ import ENC
from string import join

def getElementContent(node):
    s = ''
    for e in node.childNodes:
        if e.nodeType == node.TEXT_NODE:
            s = s + e.data
    return s


class cDocument:
    def __init__(self, doc):
        self.url = doc.getAttribute('url')
        self.list = {}
        for e in doc.childNodes:
            if e.nodeType <> e.ELEMENT_NODE: continue
            name = e.tagName
            if name not in self.list.keys():
                self.list[name] = []
            self.list[name].append(getElementContent(e))

    def print_doc(self):
        print '<div>'
        print '<strong>URL:</strong> <a href="' + self.url.encode(ENC) + '">' + self.url.encode(ENC) + '</a><br>'
        for key, val in self.list.items():
            print '<strong>' + key.encode(ENC) + '</strong>: '
            print join(val, '; ').encode(ENC)
            print '<br>'
        print '</div><br>'

