python如何使用sax提取xml配置文件内容
请问这样的XML文件如何使用sax读取啊?提取成一个字典<Config><YongShiAdditionID="1"YongzheNum="100"YongzheLv="...
请问这样的XML文件如何使用sax读取啊?
提取成一个字典
<Config>
<YongShiAddition ID="1" YongzheNum="100" YongzheLv="50" Attributes="119,50|130,50|141,50|152,50|163,50|116,0.015" Zhanhun="34000" />
<YongShiAddition ID="2" YongzheNum="100" YongzheLv="60" Attributes="119,100|130,200|141,100|152,100|163,100|116,0.03" Zhanhun="68000" />
<YongShiAddition ID="3" YongzheNum="100" YongzheLv="70" Attributes="119,150|130,150|141,150|152,150|163,150|116,0.045" Zhanhun="102000" />
<YongShiAddition ID="4" YongzheNum="100" YongzheLv="80" Attributes="119,200|130,200|141,200|152,200|163,200|116,0.06" Zhanhun="170000" />
<YongShiAddition ID="5" YongzheNum="100" YongzheLv="90" Attributes="119,250|130,250|141,250|152,250|163,250|116,0.075" Zhanhun="204000" />
<YongShiAddition ID="6" YongzheNum="100" YongzheLv="100" Attributes="119,300|130,300|141,300|152,300|163,300|116,0.09" Zhanhun="238000" />
<YongShiAddition ID="7" YongzheNum="100" YongzheLv="110" Attributes="119,350|130,350|141,350|152,350|163,350|116,0.105" Zhanhun="272000" />
</Config>
下面写的不好使:
from xml import sax
class TestHandler(sax.ContentHandler): # 定义自己的handler类,继承sax.ContentHandler
def __init__(self):
sax.ContentHandler.__init__(self) # 父类和子类都需要初始化(做一些变量的赋值操作等)
self._content =""
def startElement(self, name, attrs): # 遇到<tag>标签时候会执行的方法,这里的name,attrs不用自己传值的(这里其实是重写)
self._tag = name
if name == "Config":
print("==================")
def endElement(self, name):
if name == "Config":
print(self._content)
else:
pass
def characters(self, content): # 获取标签内容
self._content=content
if __name__ == "__main__":
handler = TestHandler() # 自定义类实例化成对象
sax.parse("test.xml", handler) # 解析xml文件 展开
提取成一个字典
<Config>
<YongShiAddition ID="1" YongzheNum="100" YongzheLv="50" Attributes="119,50|130,50|141,50|152,50|163,50|116,0.015" Zhanhun="34000" />
<YongShiAddition ID="2" YongzheNum="100" YongzheLv="60" Attributes="119,100|130,200|141,100|152,100|163,100|116,0.03" Zhanhun="68000" />
<YongShiAddition ID="3" YongzheNum="100" YongzheLv="70" Attributes="119,150|130,150|141,150|152,150|163,150|116,0.045" Zhanhun="102000" />
<YongShiAddition ID="4" YongzheNum="100" YongzheLv="80" Attributes="119,200|130,200|141,200|152,200|163,200|116,0.06" Zhanhun="170000" />
<YongShiAddition ID="5" YongzheNum="100" YongzheLv="90" Attributes="119,250|130,250|141,250|152,250|163,250|116,0.075" Zhanhun="204000" />
<YongShiAddition ID="6" YongzheNum="100" YongzheLv="100" Attributes="119,300|130,300|141,300|152,300|163,300|116,0.09" Zhanhun="238000" />
<YongShiAddition ID="7" YongzheNum="100" YongzheLv="110" Attributes="119,350|130,350|141,350|152,350|163,350|116,0.105" Zhanhun="272000" />
</Config>
下面写的不好使:
from xml import sax
class TestHandler(sax.ContentHandler): # 定义自己的handler类,继承sax.ContentHandler
def __init__(self):
sax.ContentHandler.__init__(self) # 父类和子类都需要初始化(做一些变量的赋值操作等)
self._content =""
def startElement(self, name, attrs): # 遇到<tag>标签时候会执行的方法,这里的name,attrs不用自己传值的(这里其实是重写)
self._tag = name
if name == "Config":
print("==================")
def endElement(self, name):
if name == "Config":
print(self._content)
else:
pass
def characters(self, content): # 获取标签内容
self._content=content
if __name__ == "__main__":
handler = TestHandler() # 自定义类实例化成对象
sax.parse("test.xml", handler) # 解析xml文件 展开
1个回答
展开全部
#vim info.xml
<info>
<intro>信息</intro>
<list id='001'>
<head>auto_userone</head>
<name>Jordy</name>
</list>
</info>
#vim infoxmlparser.py
#!/usr/bin/python
#coding=utf-8
#Python Version Python 2.6.5
from xml.sax.handler import ContentHandler
from xml.sax import parse
class HeadlineHandler(ContentHandler):
in_headline=False
def __init__ (self):
ContentHandler.__init__(self)
self.data=[]
self.file=open('info.txt','w')
self.file.writelines('id'+'\t'+'head\n')
self.file.close()
def startElement(self,name,attrs):
if name == 'list':
self.data.append(attrs['id'])
if name == 'head':
self.in_headline=True
def endElement(self,name):
if name == 'list':
self.file=open('info.txt','a')
self.file.writelines(self.data[0]+'\t'+self.data[1]+'\n')
self.file.close()
self.data=[]
if name == 'head':
self.in_headline=False
def characters(self,string):
if self.in_headline:
self.data.append(string)
if __name__=='__main__':
parse('info.xml',HeadlineHandler())
<info>
<intro>信息</intro>
<list id='001'>
<head>auto_userone</head>
<name>Jordy</name>
</list>
</info>
#vim infoxmlparser.py
#!/usr/bin/python
#coding=utf-8
#Python Version Python 2.6.5
from xml.sax.handler import ContentHandler
from xml.sax import parse
class HeadlineHandler(ContentHandler):
in_headline=False
def __init__ (self):
ContentHandler.__init__(self)
self.data=[]
self.file=open('info.txt','w')
self.file.writelines('id'+'\t'+'head\n')
self.file.close()
def startElement(self,name,attrs):
if name == 'list':
self.data.append(attrs['id'])
if name == 'head':
self.in_headline=True
def endElement(self,name):
if name == 'list':
self.file=open('info.txt','a')
self.file.writelines(self.data[0]+'\t'+self.data[1]+'\n')
self.file.close()
self.data=[]
if name == 'head':
self.in_headline=False
def characters(self,string):
if self.in_headline:
self.data.append(string)
if __name__=='__main__':
parse('info.xml',HeadlineHandler())
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询