日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python寫xml文件的操作實例

系統 1882 0

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

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

                        
              
                
                  
                     
        ma 
      
                  
                  
                     
        xiaoju 
      
                  
                
                
                   
      Springs Widgets, Inc. 
    
                
              
              
                
                   
      I think widgets are greate.You should buy lots of them forom 
      
                  
                     
        Spirngy Widgts, Inc 
      
                  
                
              
            
          

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程序設計有所幫助。


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 双辽市| 东至县| 社会| 亳州市| 军事| 政和县| 三穗县| 德惠市| 武宣县| 罗田县| 刚察县| 井研县| 大埔区| 黄平县| 连云港市| 高州市| 阳江市| 乐陵市| 开原市| 兰州市| 三亚市| 东乡| 南丹县| 盖州市| 施甸县| 安徽省| 垫江县| 克什克腾旗| 额尔古纳市| 赤壁市| 林州市| 临潭县| 高碑店市| 锦屏县| 旬邑县| 石嘴山市| 太保市| 报价| 揭阳市| 大宁县| 桦甸市|