PullDOM with Python Tutorial/Documentation or Code Sample
The PullDOM documentation is pretty dismal, especially because it’s a library worthwhile learning. If you have to process a large XML document (having a huge DOM tree can consume a lot of memory), but prefer not to use a SAX style interface, because the code can be difficult to understand, then PullDOM is your man.
With PullDOM, you keep looping until you hit an element that you are interested in. However, the node that you have is lazily created, and doesn’t have any childnodes. To access the child nodes in DOM-style access, you call expandNode().
Here’s a working sample.
import xml.dom.pulldom as p
events = p.parseString("<Sites><Site><Name>Jello</Name><URL>http://jello.com</URL></Site></Sites>")
for event in events:
type, node = event
if type == 'START_ELEMENT' and node.nodeName == 'Site':
events.expandNode(node)
print node.toprettyxml()
Output:
<Site>
<Name>
Jello
</Name>
<URL>
http://jello.com
</URL>
</Site>
Update: Jeff Winkler illustrates how to use Python to parse using SAX without PullDOM here.
About this entry
You’re currently reading “ PullDOM with Python Tutorial/Documentation or Code Sample ,” an entry on Chui's Counterpoint
- Published:
- 4.25.07 / 11am
- Category:
- Python
No comments
Jump to comment form | comments rss [?] | trackback uri [?]