python 寫xml檔案的操作

2021-06-06 19:20:24 字數 1054 閱讀 4389

要生成的xml檔案格式如下:

<?xml version="1.0" ?>

sample xml thing ma

xiaoju

springs widgets, inc.

first

i think widgets are greate.you should buy lots of them forom

spirngy widgts, inc

**:

from xml.dom import minidom, node

doc = minidom.document()

#generate the book

book = doc.createelement('book')

#the title

title = doc.createelement('title')

#the author section

author = doc.createelement("author")

name = doc.createelement('name')

firstname = doc.createelement('first')

lastname = doc.createelement('last')

affiliation = doc.createelement("affiliation")

#the chapter

chapter = doc.createelement('chapter')

chapter.setattribute('number', '1')

title = doc.createelement('title')

para = doc.createelement('para')

you should buy lots of them forom"))

company = doc.createelement('company')

print doc.toprettyxml()

python的xml檔案操作

python中xml的操作可以使用dom庫 sax庫 etree庫 這個是新的 parsers庫。其中dom.minidom是dom輕量級模組,這裡介紹dom.minidom和etree兩個模組這裡以userinfo.xml檔案為例,xml 內容如下 xml解析 xmldoc minidom.par...

Python 操作XML檔案

1 xml 建立 1 import xml.etree.elementtree as et 23 new xml et.element personinfolist 最外面的標籤名 4 personinfo et.subelement new xml,personinfo attrib 對應的引數是...

Python操作XML檔案 XML概述

xml 可擴充套件標置語言,為html 超文字標置語言 的補充。html用於顯示資料,而xml用於傳輸和儲存資料 一.xml語法 xml檔案通常分為兩部分 檔案宣告和檔案主體 檔案宣告 位於第一行 version標明此檔案所用的標準的版本號,必須要有 encoding標明此檔案中所使用的字元型別,可...