zoukankan      html  css  js  c++  java
  • 【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 = ' ')
  • 相关阅读:
    [题解]luogu_P4198_楼房重建(线段树logn合并
    [题解]luogu_P3084(单调队列dp
    [题解]luogu_P3084(差分约束/梦想spfa
    [题解/模板]POJ_1201(差分约束
    [题解]luogu_P5059(矩乘
    [题解]luogu_P5004跳房子2(矩乘递推
    CF1042A Benches(二分答案)
    8.24考试总结
    NOIP2017题目
    「LG3045」「USACO12FEB」牛券Cow Coupons
  • 原文地址:https://www.cnblogs.com/bombe1013/p/3708459.html
Copyright © 2011-2022 走看看