【Python Network】使用DOM生成XML

时间:2021-04-05 14:32:48

单纯的为DOM树添加结点。

 #!/usr/bin/env python
# Generating XML with DOM - Chapter 8 - domgensample.py from xml.dom import minidom, Node doc = minidom.Document() doc.appendChild(doc.createComment("Sample XML Document - Chapter 8 -")) # Generate book
book = doc.createElement('book')
doc.appendChild(book) # The title
title = doc.createElement('title')
title.appendChild(doc.createTextNode('Sample XML Thing'))
book.appendChild(title) # The author section
author = doc.createElement('author')
book.appendChild(author)
name = doc.createElement('name')
author.appendChild(name)
firstname = doc.createElement('first')
name.appendChild(firstname)
firstname.appendChild(doc.createTextNode('Benjamin'))
name.appendChild(doc.createTextNode(' '))
lastname = doc.createElement('last')
name.appendChild(lastname)
lastname.appendChild(doc.createTextNode('Smith')) affiliation = doc.createElement('affiliation')
author.appendChild(affiliation)
affiliation.appendChild(doc.createTextNode('Spring Widgets, Inc.')) # The chapter
chapter = doc.createElement('chapter')
book.appendChild(chapter)
chapter.setAttribute('number', '')
title = doc.createElement('title')
chapter.appendChild(title)
title.appendChild(doc.createTextNode('First Chapter')) para = doc.createElement('para')
chapter.appendChild(para)
para.appendChild(doc.createTextNode("I think widgets are great. You" + " should buy lots of them from "))
company = doc.createElement('company')
para.appendChild(company)
company.appendChild(doc.createTextNode('Spring Widgets, Inc')) para.appendChild(doc.createTextNode('.')) print doc.toprettyxml(indent = ' ')