您的位置:首页 > 编程语言 > Python开发

使用python对xml文件实现增删改查的简单封装

2018-01-30 11:06 337 查看
 博客已经好久没有更新了,博主现在开始玩Python了,因为公司要求以后我做Python这一块,已经研究了几个星期了。

以后还会用C或者 C++的。

在去年的这段时间还在奋斗在openframeworks,这期间发生了很多事,话不多说,直接上代码。

----------------------------------------------------------------------------------------------------------------------------------

XML.py:

__author__ = 'Barton'

#coding=utf-8

import xml.etree.ElementTree as ET

#from xml.dom import minidom

class XML(object):

    

    def __init__(self, path):

        self.path = path

        #From system load xml file,getroot()get root node

        self.tree = ET.parse(self.path)

        self.root = self.tree.getroot()

        #create dict for store config

        self.tag = {}

    '''

    #unfinished,suggest use minicom, the function will make xml file be chaos

    def addTag(self, tagName, content):

        newEle = ET.Element(tagName)

        newEle.text = content

        self.root.append(newEle)

        #self.tree.write("self.path")

    #unfinished, the function will make xml file be chaos

    def delTag(self, tagName):

        for child in self.root:

            self.tag[child.tag] = child.text

        if tagName in self.tag:

            for child in self.root:

                if tagName == child.tag:

                    self.root.remove(child)

                    #self.tree.write(self.path)

            return True

        else:

            return False

    '''

    

    def setTag(self, tagName, content):

        for child in self.root:

            self.tag[child.tag] = child.text

        if tagName in self.tag:

            self.root.find(tagName).text = content

            #self.tree.write(self.path)

            return True

        else:

            return False

    def getTag(self, tagName):

        for child in self.root:

            self.tag[child.tag] = child.text

        if tagName in self.tag:

            return self.tag[tagName]

        else:

            return False

    def getAllTag(self):

        for child in self.root:

            self.tag[child.tag] = child.text

        return self.tag

    def save(self):

        self.tree.write(self.path)

if "__main__" == __name__:

    test = XML('config.ini')

    print(test.getTag('admincode'))

    print(test.getAllTag())

    test.setTag('admincode3', 'abbbbbbbbbbbbbba')

    test.save()

    print(test.getAllTag())---------------------------------------------------------------------------------------------------------------------------------

 上边实现了增删改查功能,其中增删功能会导致格式有些错乱,解决方法已经注释了。在公司的项目需求中只用到查和改功能,增和删是顺手加上的,以后如果要用的话继续去完成。下边上测试代码,及xml文件,测试在windows和linux下都没有什么问题。

----------------------------------------------------------------------------------------------------------------------------------

test.py:

from XML import XML

if "__main__" == __name__:

    test = XML('config.ini')

    print(test.getAllTag())

    print(test.getTag('adminCode'))

    test.setTag('adminCode', '123456')

    test.save()

    print(test.getTag('adminCode')

----------------------------------------------------------------------------------------------------------------------------------

config.xml:

<config>
<adminCode>pixelplus</adminCode>
<adminCode1>pixelplus1</adminCode1>
<adminCode2>pixelplus2</adminCode2>
<adminCode3>pixelplus3</adminCode3>

</config>

----------------------------------------------------------------------------------------------------------------------------------

排版有点伤。。vi用习惯了,感觉其它编辑环境都很难受(╯﹏╰)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python xml
相关文章推荐