• 【Python Network】使用DOM生成XML


    单纯的为DOM树添加结点。

     1 #!/usr/bin/env python
     2 # Generating XML with DOM - Chapter 8 - domgensample.py
     3 
     4 from xml.dom import minidom, Node
     5 
     6 doc = minidom.Document()
     7 
     8 doc.appendChild(doc.createComment("Sample XML Document - Chapter 8 -"))
     9 
    10 # Generate book
    11 book = doc.createElement('book')
    12 doc.appendChild(book)
    13 
    14 # The title
    15 title = doc.createElement('title')
    16 title.appendChild(doc.createTextNode('Sample XML Thing'))
    17 book.appendChild(title)
    18 
    19 # The author section
    20 author = doc.createElement('author')
    21 book.appendChild(author)
    22 name = doc.createElement('name')
    23 author.appendChild(name)
    24 firstname = doc.createElement('first')
    25 name.appendChild(firstname)
    26 firstname.appendChild(doc.createTextNode('Benjamin'))
    27 name.appendChild(doc.createTextNode(' '))
    28 lastname = doc.createElement('last')
    29 name.appendChild(lastname)
    30 lastname.appendChild(doc.createTextNode('Smith'))
    31 
    32 affiliation = doc.createElement('affiliation')
    33 author.appendChild(affiliation)
    34 affiliation.appendChild(doc.createTextNode('Spring Widgets, Inc.'))
    35 
    36 # The chapter
    37 chapter = doc.createElement('chapter')
    38 book.appendChild(chapter)
    39 chapter.setAttribute('number', '1')
    40 title = doc.createElement('title')
    41 chapter.appendChild(title)
    42 title.appendChild(doc.createTextNode('First Chapter'))
    43 
    44 para = doc.createElement('para')
    45 chapter.appendChild(para)
    46 para.appendChild(doc.createTextNode("I think widgets are great. You" + " should buy lots of them from "))
    47 company = doc.createElement('company')
    48 para.appendChild(company)
    49 company.appendChild(doc.createTextNode('Spring Widgets, Inc'))
    50 
    51 para.appendChild(doc.createTextNode('.'))
    52 
    53 print doc.toprettyxml(indent = ' ')
  • 相关阅读:
    网页结构的简介和Xpath语法的入门教程
    (原创)WinForm中莫名其妙的小BUG——ComboBox 尺寸高度问题
    (原创)WinForm中莫名其妙的小BUG——RichTextBox自动选择字词问题
    Docker(1)概述
    Docker(2)centos7.X下安装
    Docker(5) docker部署nginx
    Docker(3)阿里云镜像加速
    Docker(4)docker常用命令
    Docker学习路线
    weblogic重要漏洞记录
  • 原文地址:https://www.cnblogs.com/bombe1013/p/3708459.html
Copyright © 2020-2023  润新知