Python语法之Xml 解析分析

你猜 阅读:170 2021-03-31 13:38:00 评论:0

Python3 涉及XML解析功能代码:

 

import io 
import sys 
import xml.dom.minidom #dom解析依赖包 
import xml.sax #sax 解析依赖包 
import xml.etree.ElementTree as ET # ElementTree 解析依赖包 
from types import MethodType,FunctionType #判断函数是函数对象还是对象方法 
sys.stdout = io.TextIOWrapper(sys.stdout.detach(),encoding='utf-8') 
 
# Python3 支持xml 解析之dom 
# 读取文件 
dom = xml.dom.minidom.parse('test.xml') 
print(isinstance(xml.dom.minidom.parse,FunctionType)) 
# 获取文档元素对象 
data = dom.documentElement 
# 获取 student 
stus = data.getElementsByTagName('student') 
for stu in stus: 
	# 获取标签属性值 
    st_id = stu.getAttribute('id') 
    st_name = stu.getAttribute('name') 
	# 获取标签中内容 
    id = stu.getElementsByTagName('id')[0].childNodes[0].nodeValue 
    name = stu.getElementsByTagName('name')[0].childNodes[0].nodeValue 
    age = stu.getElementsByTagName('age')[0].childNodes[0].nodeValue 
    gender = stu.getElementsByTagName('gender')[0].childNodes[0].nodeValue 
    print('st_id:', st_id,  ', st_name:',st_name) 
    print('id:', id, ', name:', name, ', age:', age, ', gender:',gender) 
 
# Python3 支持xml 解析之sax 
class StudentHandler(xml.sax.ContentHandler): 
    def __init__(self): 
        self.id = "" 
        self.name = "" 
        self.age = "" 
        self.gender = "" 
 
    # 元素开始调用 
    def startElement(self, tag, attributes): 
        self.CurrentData = tag 
        if tag == "student": 
            stu_name = attributes["name"] 
            print("stu_name:", stu_name) 
 
    # 元素结束调用 
    def endElement(self, tag): 
        if self.CurrentData == "id": 
            print("id:", self.id) 
        elif self.CurrentData == "name": 
            print("name:", self.name) 
        elif self.CurrentData == "age": 
            print("age:", self.age) 
        elif self.CurrentData == "gender": 
            print("gender:", self.gender) 
        self.CurrentData = "" 
 
    # 读取字符时调用 
    def characters(self, content): 
        if self.CurrentData == "id": 
            self.id = content 
        elif self.CurrentData == "name": 
            self.name = content 
        elif self.CurrentData == "age": 
            self.age = content 
        elif self.CurrentData == "gender": 
            self.gender = content 
 
 # 创建 XMLReader 
parser = xml.sax.make_parser() 
# 关闭命名空间 
parser.setFeature(xml.sax.handler.feature_namespaces, 0) 
# 重写 ContextHandler 
Handler = StudentHandler() 
parser.setContentHandler(Handler) 
parser.parse("test.xml") 
print('id:', Handler.id, ', name:', Handler.name, ', age:', Handler.age, ', gender:',Handler.gender) 
 
# Python3 支持xml 解析之ElementTree 
tree = ET.parse("test.xml") 
# 根节点 
root = tree.getroot() 
# 标签名 
print('root_tag:',root.tag) 
for stu in root: 
    # 属性值 
    print ("stu_name:", stu.attrib["name"]) 
    # 标签中内容 
    print ("id:", stu[0].text) 
    print ("name:", stu[1].text) 
    print("age:", stu[2].text) 
    print("gender:", stu[3].text)

 

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
关注我们

一个IT知识分享的公众号