【Python】Python XML 读写
class ACTIVE_FILE_PROTECT_RULE_VIEW(APIView):
    renderer_classes = (JSONRenderer, BrowsableAPIRenderer)
    parser_classes = (JSONParser,)
    def post(self, request):
        from datetime import datetime
        from django.utils import timezone
        from django.utils.timezone import utc
        import time
        import xml.etree.ElementTree as ET
        from xml.etree.ElementTree import ElementTree,Element
        root = ET.fromstring(RULE_XML_TPL)
        fileprotect = root.find('fileprotect')
        # print fileprotect.tag, fileprotect.attrib
        user_info = request.session.get('user_info')
        customer_id = user_info.get('customer_id')
        body_data = request.body
        request_data = json.loads(body_data)
        device_hash = request_data['device_hash']
        with transaction.atomic():
            device = models.FILE_PROTECT_INSTANCE.objects.get(device_hash=device_hash)
            assert(device.customer_id == customer_id)
            rule_list = models.FILE_PROTECT_RULE_UPDATE.objects.filter(device_hash=device_hash)
            for rule in rule_list:
                tmp_rule = Element('rule', {
                    'id': str(rule.id),
                    'enabled': 'true' if rule.enable else 'false',
                    'status': 'true' if rule.apply_status else 'false',
                    'log': rule.log,
                    'opertion': ','.join(json.loads(rule.operation)),
                    'recover': 'true' if rule.recover else 'false',
                    'protectdir': rule.protectdir,
                    'action': 'allow' if rule.action else 'deny',
                    'protectfiletype': ','.join(json.loads(rule.file_type_list)),
                    'comment': rule.commont
                })
                rule.apply_status =
                rule.save()
                fileprotect.append(tmp_rule)
            # ET.dump(root)
            tmp_xml = ET.tostring(root, encoding="utf-8", method="xml")
            rule_xml = '<?xml version="1.0" encoding="utf-8"?>\n' + tmp_xml
            tmp_commit_rule_list = models.FILE_PROTECT_RULE_COMMIT.objects.filter(device_hash=device_hash).filter(customer_id=customer_id)
            # 首次入库
            if(len(tmp_commit_rule_list) == ):
                tmp_commit_rule = models.FILE_PROTECT_RULE_COMMIT(customer_id=customer_id, device_hash=device_hash, rule_xml_text=rule_xml)
                tmp_commit_rule.save()
            # 后续修改xml内容和版本号(时间戳)
            else:
                tmp_commit_rule = models.FILE_PROTECT_RULE_COMMIT.objects.get(device_hash=device_hash)
                if(tmp_commit_rule.rule_xml_text == rule_xml):
                    pass
                else:
                    tmp_commit_rule.rule_xml_text = rule_xml
                    tmp_commit_rule.version = timezone.now()
                    tmp_commit_rule.save()
            from django.forms.models import model_to_dict
            version = tmp_commit_rule.version
            tmp_commit_rule = model_to_dict(tmp_commit_rule)
            '''from datetime import datetime
            from django.utils import timezone
            from django.utils.timezone import utc
            import time'''
            #time.mktime(timezone.now().timetuple())
            version = time.mktime(version.timetuple())
            tmp_commit_rule['version'] = version
        return APIResponse(status=status_code.success, data=tmp_commit_rule)
参考资料:
XML读写
推荐:http://blog.csdn.net/gingerredjade/article/details/21944675
http://bbs.csdn.net/topics/350027413
http://bbs.csdn.net/topics/390194606
http://python.jobbole.com/82775/
http://www.jb51.net/article/67190.htm
http://blog.csdn.net/shomy_liu/article/details/37929181
http://www.python tab.com/html/2013/pythonjichu_0618/451.html
http://blog.csdn.net/xibuzhihun/article/details/6950142
http://www.cnblogs.com/CheeseZH/p/4026686.html
http://www.jb51.net/article/17687.htm
http://blog.csdn.net/kiki113/article/details/4052584
http://www.jb51.net/article/67120.htm
Django UTC时间问题
解决Python自带的json序列化工具不能序列化datetime类型数据问题:http://www.au92.com/archives/resove-python-can-not-serialize-datetime.html
http://smilejay.com/2014/06/django-datetimefield-timezone-issue/
三元表达式:
http://blog.csdn.net/lanyuanershe/article/details/8083425
JOIN LIST:http://www.jb51.net/article/63598.htm
【Python】Python XML 读写的更多相关文章
- Python之xml读写
		遇到问题xml文件读写,没有子节点需要新建ChildNode. # -*- coding: utf-8 -*- import os import shutil import xml.dom.minid ... 
- Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)
		本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 一.前言 我们在<中我们描述了Python数据持久化的大体概念和基本处理方式,通过这些知识点我们已经 ... 
- 【转】Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)
		[转]Python之xml文档及配置文件处理(ElementTree模块.ConfigParser模块) 本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 ... 
- python 生成 xml文件 属性的顺序问题
		需求很奇葩. 文档示例 <ITEM key="username" eng="User Name" chn="用户名" val=&quo ... 
- python读取xml文件
		关于python读取xml文章很多,但大多文章都是贴一个xml文件,然后再贴个处理文件的代码.这样并不利于初学者的学习,希望这篇文章可以更通俗易懂的教如何使用python 来读取xml 文件. 什么是 ... 
- python 解析XML python模块xml.dom解析xml实例代码
		分享下python中使用模块xml.dom解析xml文件的实例代码,学习下python解析xml文件的方法. 原文转自:http://www.jbxue.com/article/16587.html ... 
- python解析xml模块封装代码
		在python中解析xml文件的模块用法,以及对模块封装的方法.原文转自:http://www.jbxue.com/article/16586.html 有如下的xml文件:<?xml vers ... 
- python解析xml之lxml
		虽然python解析xml的库很多,但是,由于lxml在底层是用C语言实现的,所以lxml在速度上有明显优势.除了速度上的优势,lxml在使用方面,易用性也非常好.这里将以下面的xml数据为例,介绍l ... 
- python处理xml的常用包(lib.xml、ElementTree、lxml)
		python处理xml的三种常见机制 dom(随机访问机制) sax(Simple APIs for XML,事件驱动机制) etree python处理xml的三种包 标准库中的xml Fredri ... 
随机推荐
- Git代码管理心得
			一.概述: 这次按照要求进行了看似复杂,实则非常复杂并且麻烦(网上教程众多且啰嗦)的对git使用的学习,从星期六晚18:48我准备这次作业开始,直到了晚上22:44才结束电脑上的操作···(导致这篇随 ... 
- Red Hat Linux9命令行--修改补充中
			1.使用rpm工具安装应用软件:rpm [选项] [软件包名] 常用的参数及含义如下图所示: 2.编译安装应用软件 (1).tar.gz和.tgz使用如下的命令: [root@myhost ro ... 
- Linux中使用crontab命令定时执行shell脚本或其他Linux命令
			使用crontab你可以在指定的时间执行一个shell脚本或者一系列Linux命令.例如系统管理员安排一个备份任务使其每天都运行 如何往 cron 中添加一个作业? # crontab –e0 5 * ... 
- 【BZOJ 2002】【Hnoi 2010】弹飞绵羊   分块||Link Cut Tree  两种方法
			ShallWe,Yveh,hmy,DaD3zZ,四人吃冰糕从SLYZ超市出来后在马路上一字排开,,,吃完后发现冰糕棍上写着:“向狮子座表白:愿做你的小绵羊”,,, 好吧在这道题里我们要弹飞绵羊,有分块 ... 
- 50行代码仿backbone_todos
			<!doctype html> <html> <head> <meta charset="utf-8"> <title> ... 
- poj2485&&poj2395 kruskal
			题意:最小生成树的最大边最小,sort从小到大即可 poj2485 #include<stdio.h> #include<string.h> #include<algor ... 
- C++ 11 线程的同步与互斥
			这次写的线程的同步与互斥,不依赖于任何系统,完全使用了C++11标准的新特性来写的,就连线程函数都用了C++11标准的lambda表达式. /* * thread_test.cpp * * Copyr ... 
- Oracle导出导入数据库的方式
			一.导入导出.dmp文件 利用cmd的操作命令导出,详情如下(备注:方法二是转载网上的教程):1:G:\Oracle\product\10.1.0\Client_1\NETWORK\ADMIN目录下有 ... 
- Spring POST
			Spring POST+表单对象如果数据格式不正确,则页面直接报400.页面没有反应,烦啊 
- 配置Junit测试程序
			第一步:加载所需要的包:右键-->Build Path-->Configure Build Path-->Libraries-->Add Library-->Junit ... 
