使用python3.4解析xml文件(sax、dom、etree)
发布时间:2020-05-29 00:10:54 所属栏目:Python 来源:互联网
导读:使用python3.4解析xml文件(sax、dom、etree)
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 #重载了三个方法
#处理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())
<?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(" "*4*level,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)
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> import xml.etree.ElementTree as et
x = et.Element("name")
x.text = "dengjingdong"
x.set("boy","true")
sx = et.tostring(x)
print(sx)
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
