【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 ...
随机推荐
- 每天一个linux命令(26):du 命令
Linux du命令也是查看使用空间的,但是与df命令不同的是Linux du命令是对文件和目录磁盘使用的空间的查看,还是和df命令有一些区别的. 1.命令格式: du [选项][文件] 2.命令功能 ...
- javamail技术
package com.zh.javaEmail; import java.util.*; import javax.mail.*; import javax.mail.internet.*; imp ...
- shell--题目
1.有一个文件,里面有二列,第一列ip地址,第二列是时间,同一个ip可能出现多次,但时间不同. 文件类似下面的样子: 192.168.1.2 13:10 192.127.12 ...
- hasSet,TreeSet,ArrayList,LinkedList,Vector,HashMap,HashTable,TreeMap利用Iterator进行输出
基础类,没有重写hashCode()和equals()方法: package niukewang; import java.util.Objects; public class setClass { ...
- oracle基本语句
ALTER TABLE SCOTT.TEST RENAME TO TEST1--修改表名 ALTER TABLE SCOTT.TEST RENAME COLUMN NAME TO NAME1 --修改 ...
- Oracle自定义函数1
用户定义函数是存储在数据库中的代码块,可以把值返回到调用程序.调用时如同系统函数一样,如max(value)函数,其中,value被称为参数.函数参数有3种类型. IN 参数类型:表示输入给函数的参数 ...
- 积木(DP)问题
问题:Do you remember our children time? When we are children, we are interesting in almost everything ...
- BZOJ-3227 红黑树(tree) 树形DP
个人认为比较好的(高端)树形DP,也有可能是人傻 3227: [Sdoi2008]红黑树(tree) Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1 ...
- Hession矩阵与牛顿迭代法
1.求解方程. 并不是所有的方程都有求根公式,或者求根公式很复杂,导致求解困难.利用牛顿法,可以迭代求解. 原理是利用泰勒公式,在x0处展开,且展开到一阶,即f(x) = f(x0)+(x-x0)f' ...
- POJ2299 Ultra-QuickSort
Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...