minidom use cases


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. from xml.dom import minidom
  2.  
  3. #doc = minidom.parse(file)
  4. doc = minidom.parseString("<a>hello</a>")
  5.  
  6.  
  7. print doc.toprettyxml(encoding="utf-8")
  8.  
  9. node=doc.createElement("b")
  10. node.setAttribute("nom","val")
  11. doc.documentElement.appendChild(node)
  12.  
  13. node2=doc.createTextNode("koko")
  14. doc.documentElement.appendChild(node2)
  15.  
  16.  
  17. print doc.toprettyxml(encoding="utf-8")
  18.  
  19. for i in doc.documentElement.childNodes:
  20. print i,i.nodeType #(i.ELEMENT_NODE,i.TEXT_NODE)
  21.  
  22. for i in doc.getElementsByTagName("a"):
  23. print "-<A>-",i
  24.  
  25. node.parentNode.removeChild(node)
  26. node2.parentNode.removeChild(node2)
  27.  
  28. print doc.toprettyxml(encoding="utf-8")

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.