zoukankan      html  css  js  c++  java
  • python 读取xml文档

    原文地址:http://www.cnblogs.com/lgcf/archive/2009/09/25/1573863.html

    test.py

    from xml.dom.minidom import parse,parseString
    class XmlConfig:
    def __init__(self,path):
    self.xmlData=self.GetXml(path)
    def GetText(self,nodelist):
    r=""
    for nxd in nd.childNodes:
    r=r+nxd.nodeValue
    return r
    def GetXml(self,path):
    doc=parse(path)
    st=doc.firstChild
    websites= st.childNodes

    lstList=[]
    for sw in websites:
    if sw.nodeType==sw.ELEMENT_NODE :
    lsty=[]
    for nd in sw.childNodes:
    if nd.nodeType==nd.ELEMENT_NODE:
    ndName= nd.nodeName
    ndValue= nd.firstChild.data
    b=(ndName,ndValue)
    lsty.append(b)
    lstList.append(lsty)
    return lstList

    def GetSingle(self,siteName):
    for item in self.xmlData:
    for k,v in item:
    if v==siteName:
    return item

    def GetSingleDict(self,siteName):
    lst=self.GetSingle(siteName)
    dic={}
    if len(lst)>0:
    for item in lst:
    dic[item[0]]=item[1]
    return dic

    if __name__=="__main__":
    f=XmlConfig("test.xml")
    print f.xmlData

    test.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <Site>
    <WebSites>
    <website>http://www.xxx.net</website>
    <loginurl>http:///www.xxx.net/login.php</loginurl>
    <username>uname=xxx</username>
    <passwd>pass=123456</passwd>
    <other><![CDATA[r=5&remember=0&ur=xxx]]></other>
    <config>WebSite.ini</config>
    <configname>XXX</configname>
    </WebSites>
    <WebSites>
    <website>http://www.xxx.com</website>
    <loginurl>http:///www.xxx.com/login.php</loginurl>
    <username>uname=xxx</username>
    <passwd>pass=123456</passwd>
    <other><![CDATA[r=5&remember=0&ur=xxx]]></other>
    <config>WebSite.ini</config>
    <configname>XXX</configname>
    </WebSites>
    </Site>

    结果:

    m@m-desktop:~/.wkl/a$ python test.py 
    [[(u'website', u'http://www.xxx.net'), (u'loginurl', u'http:///www.xxx.net/login.php'), (u'username', u'uname=xxx'), (u'passwd', u'pass=123456'), (u'other', u'r=5&remember=0&ur=xxx'), (u'config', u'WebSite.ini'), (u'configname', u'XXX')], [(u'website', u'http://www.xxx.com'), (u'loginurl', u'http:///www.xxx.com/login.php'), (u'username', u'uname=xxx'), (u'passwd', u'pass=123456'), (u'other', u'r=5&remember=0&ur=xxx'), (u'config', u'WebSite.ini'), (u'configname', u'XXX')]]







  • 相关阅读:
    mysql工具导出数据库表数据
    c#接收http的post请求的多个文件流
    java上传文件和参数到服务器
    windows server 2008 w3svc服务无法启动
    java调用c#webapi的接口实现文件上传
    postman上线文件上传,并用c#服务端接收
    sql语句修改数据库字段的长度
    VB2015运行项目时出现的错误
    JavaWeb实现分页功能
    会话跟踪技术
  • 原文地址:https://www.cnblogs.com/wangkangluo1/p/2186460.html
Copyright © 2011-2022 走看看