Last active
August 29, 2015 14:13
-
-
Save oneyoung/0121207433b19fd190de to your computer and use it in GitHub Desktop.
[python] simple XML DOM parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from xml.dom.minidom import parse, parseString | |
dom1 = parse('c:\\temp\\mydata.xml') # parse an XML file by name | |
datasource = open('c:\\temp\\mydata.xml') | |
dom2 = parse(datasource) # parse an open file | |
dom3 = parseString('<myxml>Some data<empty/> some more data</myxml>') | |
##################################################################### | |
import xml.dom.minidom | |
document = """\ | |
<slideshow> | |
<title>Demo slideshow</title> | |
<slide><title>Slide title</title> | |
<point>This is a demo</point> | |
<point>Of a program for processing slides</point> | |
</slide> | |
<slide><title>Another demo slide</title> | |
<point>It is important</point> | |
<point>To have more than</point> | |
<point>one slide</point> | |
</slide> | |
</slideshow> | |
""" | |
dom = xml.dom.minidom.parseString(document) | |
def getText(nodelist): | |
rc = [] | |
for node in nodelist: | |
if node.nodeType == node.TEXT_NODE: | |
rc.append(node.data) | |
return ''.join(rc) | |
def handleSlideshow(slideshow): | |
print "<html>" | |
handleSlideshowTitle(slideshow.getElementsByTagName("title")[0]) | |
slides = slideshow.getElementsByTagName("slide") | |
handleToc(slides) | |
handleSlides(slides) | |
print "</html>" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment