【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 ...
随机推荐
- beta版本贡献率
队名:攻城小分队 031302410 郭怡锋 : 占比:50% 031302411 洪大钊: 占比:30% 031302206 陈振贵: 占比:10% 031302416 黄伟祥: 占比:10%
- iOS7(iPhone4)button不能改变button的title
最近整了个高端的iPhone4测试机,系统是iOS7.1,测出一个问题,两个button,第二个的enable为NO,点击第一个button,第二个的title改变,然而在iPhone4上并不能运行, ...
- 【BZOJ 3545】【ONTAK 2010】Peaks & 【BZOJ 3551】【ONTAK 2010】Peaks加强版 Kruskal重构树
sunshine的A题我竟然调了一周!!! 把循环dfs改成一个dfs就可以,,,我也不知道为什么这样就不会RE,但它却是A了,,, 这周我一直在调这个题,总结一下智障错误: 1.倍增的范围设成了n而 ...
- Struts tag -s
1,if/elseif/else标签 <s:set value="19"/> <s:if test="%{#age > 60}"> ...
- BZOJ-1143&&BZOJ-2718 祭祀river&&毕业旅行 最长反链(Floyed传递闭包+二分图匹配)
蛋蛋安利的双倍经验题 1143: [CTSC2008]祭祀river Time Limit: 10 Sec Memory Limit: 162 MB Submit: 1901 Solved: 951 ...
- jdbcTemplate的配置
相关jar 包 package sfk.bbs.test.springjsbctempletTest; import static org.junit.Assert.*; import java.sq ...
- App接口中json方式封装通信接口
封装json通信接口的类 <?php class Response{ /** * 按json方式输出通信数据 * @param integer $code状态码 * @param string ...
- Get与Post比较
- rsyslog 与 logrotate 服务
rsyslog与logrotate服务 rsyslog 负责写入日志, logrotate负责备份和删除旧日志, 以及更新日志文件. 一.rsyslog rsyslog 是一个 syslogd 的多线 ...
- js中window的属性
Window的属性 属性描述 closed 获取引用窗口是否已关闭. defaultStatus 设置或获取要在窗口底部的状态栏上显示的缺省信息. dialogArguments 设置或获取传递给模式 ...