使用python3.4解析xml文件(sax、dom、etree)
調用sax模塊處理xml文件。
#重載了三個方法處理xml,主要就是寫自己的事件處理類
from xml.sax import *
class DengHandler(ContentHandler): def startDocument(self): print("----開始解析xml文檔----") def endDocument(self): print("----xml文檔解析完畢----") def startElement(self,name,attrs): if name == "author": print("名字:",attrs['name']," 日期:",attrs["birth"])
parse("deng.xml",DengHandler()) </pre>
deng.xml
<?xml version = "1.0" encoding = "utf-8"?> <author name = "dengjingdong" birth = "19920517"></author> </people>
調用dom模塊中的minidom處理xml文件。
from xml.dom.minidom import *scannode函數打印xml文件的結構
def scannode(doc,level = 0): ret = doc.class.name if doc.nodeType == Node.ELEMENT_NODE: ret += ",標簽:" + doc.tagName print(" "4level,ret) if doc.hasChildNodes: for child in doc.childNodes: scannode(child,level+1)
----scannode-----
xin = parse("book.xml") print(xin) scannode(xin)
----scannode-----
x = parse("domtest.xml") nx = x.getElementsByTagName("author")
print(nx[0].getAttribute("birth")) print(nx[0].childNodes[0].data)
print(nx[1].getAttribute("birth")) print(nx[1].childNodes[0].data)</pre>
book.xml
<?xml version = "1.0" encoding = "utf-8" ?> <book> <title>the book title</title> <author> <name>jingdong</name> <boy>true</boy> </author> <chapter number = "1"> <title> first chapter </title> <para> I love python. </para> </chapter> </book>
domtest.xml
<?xml version = "1.0" encoding = "utf-8" ?> <people> <author name = "dengjingdong" birth = "1990517">dongdong</author> <author name = "wushengnan" birth = "19920520">nannan</author> </people>
調用etree模塊中的ElementTree生成所需的xml文件。
import xml.etree.ElementTree as et x = et.Element("name") x.text = "dengjingdong" x.set("boy","true") sx = et.tostring(x) print(sx)