CARVIEW |
MiniDom
Some notes on how to use xml.dom.minidom:
1 from xml.dom.minidom import parse, parseString
2
3 dom1 = parse( "foaf.rdf" ) # parse an XML file
4 dom2 = parseString( "<myxml>Some data <empty/> some more data</myxml>" )
5 print dom1.toxml()
6 print dom2.toxml()
Examples of Use
node.nodeName
node.nodeValue
node.childNodes
Find Elements
You can manually walk through the childNodes tree, comparing nodeNames.
You might be able to use getElementsByTagName as well:
1 from xml.dom.minidom import parse
2 dom = parse("foo.xml")
3 for node in dom.getElementsByTagName('bar'): # visit every node <bar />
4 print node.toxml()
getElementsByTagName, works recursively into the tree, I believe.
Add an Element
Create & add an XML element (Something like <foo />) to an XML document.
1 from xml.dom.minidom import parse
2 dom = parse("bar.xml")
3 x = dom.createElement("foo") # creates <foo />
4 dom.childNodes[1].appendChild(x) # appends at end of 1st child's children
5 print dom.toxml()
Add an Element with Text Inside
Create & add an XML element to an XML document, the element has text inside.
ex: <foo>hello, world!</foo>
1 from xml.dom.minidom import parse
2 dom = parse("bar.xml")
3 x = dom.createElement("foo") # creates <foo />
4 txt = dom.createTextNode("hello, world!") # creates "hello, world!"
5 x.appendChild(txt) # results in <foo>hello, world!</foo>
6 dom.childNodes[1].appendChild(x) # appends at end of 1st child's children
7 print dom.toxml()
Import a Node
You can use DOM 2 "importNode" to take part of one XML document, and put it into another XML document.
1 from xml.dom.minidom import parse
2 dom1 = parse("foo.xml")
3 dom2 = parse("bar.xml")
4 x = dom1.importNode(dom2.childNodes[1], # take 2nd node in "bar.xml"
5 True) # deep copy
6 dom1.childNodes[1].appendChild(x) # append to children of 2nd node in "foo.xml"
7 print dom1.toxml()
Links
Python Library Reference, xml.dom.minidom -- API documentation
Dive into Python, Chapter 5 -- works almost entirely out of the minidom API
See Also
EditText (last edited 2005-02-12 22:13:43 by LionKimbro)
DeleteCache (cached 2007-08-05 17:03:47)- Login
- Navigation
- Actions
- Your recent pages