Python的XML解析!

#   解析XML内容
from xml.sax import parse               #   分析文本用的模块
from xml.sax import ContentHandler      #   内容处理类
# <root>
#     <student id='张三'>
#         <name>张三</name>
#         <sex>男</sex>
#         <age>20</age>
#     </student>
class MyPar(ContentHandler):
    def __init__(self):
        ContentHandler.__init__(self)
        self.tag_name   = ''    # 元素标签
self.count_name = ''    # 张三
self.count_sex  = ''    # 性别
self.count_age  = ''    # 年龄
def startElement(self, name, attrs):
        self.tag_name = name    #   获取标签名
if self.tag_name == 'student':
            print('*'*20)
            print("id:",attrs['id'])

    def characters(self, content):
        if self.tag_name == 'name':
            self.count_name = content

        if self.tag_name == 'sex':
            self.count_sex = content

        if self.tag_name == 'age':
            self.count_age = content

    def endElement(self, name):
        if name == 'name':
            print("   name:",self.count_name)

        if name == 'sex':
            print("    sex:",self.count_sex)

        if name == 'age':
            print("    age:", self.count_age)

        self.tag_name = ''
if __name__ == '__main__':
    p = MyPar()
    parse('stu.xml',p)




from xml.sax import parse               #   导入解析包(分析文本用的模块)
from xml.sax import ContentHandler      #   导入内容处理类
# <root name="中国">
#   <province name="北京市">
#     <city name="北京市" index="1">
#       <area name="东城区" index="1" />
class MyHader(ContentHandler):
    def __init__(self):
        ContentHandler.__init__(self)
        self.tag_name = ''
def startElement(self, name, attrs):
        self.tag_name = name
        if self.tag_name == 'root':
            print("国家:",attrs['name'])

        if self.tag_name == 'province':
            print("  省:",attrs['name'])

        if self.tag_name == "city":
            print("      市:",attrs['name'],attrs['index'])

        if self.tag_name == "area":
            print("          区(县):",attrs['name'],attrs['index'])

    def endElement(self, name):
        self.tag_name = ''
if __name__ == '__main__':
    p = MyHader()
    parse('city.xml',p)
posted @ 2018-06-01 20:57  非洲小白脸non  阅读(263)  评论(0编辑  收藏  举报