buhao 发表于 2017-4-23 11:27:27

python 解析xml

使用:xml.dom
1:导入包:
 
import xml.dom.minidom
 2:读取
 
从文件:
dom1=xml.dom.minidom.parse('book.xml')
从url
maps=urllib.urlopen(url).read()
doc = xml.dom.minidom.parseString(maps)
 3:编码
     python:# -*- coding: utf-8 -*-
     xml编码:<?xml version="1.0" encoding="GB2312"?>
 
maps=maps.replace('<?xml version="1.0" encoding="GB2312"?>','<?xml version="1.0" encoding="utf-8"?>')
 4:一些方法
 
minidom.parse(filename):加载读取XML文件
doc.documentElement:获取XML文档对象
node.getAttribute(AttributeName):获取XML节点属性值
node.getElementsByTagName(TagName):获取XML节点对象集合
node.childNodes :返回子节点列表。
node.childNodes.nodeValue:获取XML节点值
node.firstChild:访问第一个节点,等价于pagexml.childNodes
 5:返回Node节点的xml表示的文本
 
doc.toxml('UTF-8')
更新中。。。
 
 
页: [1]
查看完整版本: python 解析xml