国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python寫xml文件的操作實例

2019-11-25 18:09:33
字體:
來源:轉載
供稿:網友

本文實例講述了python寫xml文件的操作的方法,分享給大家供大家參考。具體方法如下:

要生成的xml文件格式如下:

<?xml version="1.0" ?> <!--Simple xml document__chapter 8--> <book>   <title>     sample xml thing   </title>   <author>     <name>       <first>         ma       </first>       <last>         xiaoju       </last>     </name>     <affiliation>       Springs Widgets, Inc.     </affiliation>   </author>   <chapter number="1">     <title>       First     </title>     <para>       I think widgets are greate.You should buy lots of them forom       <company>         Spirngy Widgts, Inc       </company>     </para>   </chapter> </book> 

Python實現代碼如下:

from xml.dom import minidom, Node  doc = minidom.Document()  doc.appendChild(doc.createComment("Simple xml document__chapter 8"))  #generate the 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') firstname.appendChild(doc.createTextNode("ma")) name.appendChild(firstname) lastname = doc.createElement('last') name.appendChild(lastname) lastname.appendChild(doc.createTextNode("xiaoju"))  affiliation = doc.createElement("affiliation") affiliation.appendChild(doc.createTextNode("Springs Widgets, Inc.")) author.appendChild(affiliation)  #The chapter chapter = doc.createElement('chapter') chapter.setAttribute('number', '1') title = doc.createElement('title') title.appendChild(doc.createTextNode("First")) chapter.appendChild(title) book.appendChild(chapter)  para = doc.createElement('para') para.appendChild(doc.createTextNode("I think widgets are greate./ You should buy lots of them forom")) company = doc.createElement('company') company.appendChild(doc.createTextNode("Spirngy Widgts, Inc")) para.appendChild(company) chapter.appendChild(para)  print doc.toprettyxml() 

希望本文所述對大家的Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 朝阳区| 宁晋县| 图木舒克市| 田林县| 桂林市| 滨州市| 盐源县| 高青县| 织金县| 电白县| 新乐市| 桐乡市| 靖安县| 卫辉市| 保靖县| 东明县| 肃宁县| 鄯善县| 麻江县| 日喀则市| 富平县| 从化市| 新民市| 台中县| 广饶县| 临澧县| 祥云县| 涿鹿县| 津南区| 南开区| 铁力市| 罗江县| 三穗县| 南川市| 射洪县| 昌平区| 抚顺市| 高青县| 龙海市| 阿勒泰市| 四子王旗|