python处理xml文件的方法小结
作者:Flying_tao 时间:2023-10-28 01:53:33
本文实例讲述了python处理xml文件的方法。分享给大家供大家参考,具体如下:
前一段时间因为工作的需要,学习了一点用Python处理xml文件的方法,现在贴出来,供大家参考。
xml文件是按节点一层一层来叠加的,最顶层的是根节点。比如说:
<sys:String x:Key="STR_License_WithoutLicense">Sorry, you are not authorized.</sys:String>
其中sys:String为节点名字,x:Key的内容为Attribute,xml节点值为sys:String的子节点,它是文本节点类型。<节点名称 x:Key="Attribute">子节点。。。
RPD的xml格式:
<ResourceDictionary>
<sys:String x:Key="STR_Startup_LaunchRPD">Launching Polycom RealPresence Desktop</sys:String>
<sys:String x:Key="STR_Startup_CheckFolder">Checking folder</sys:String>
CMAD的xml格式:
<language-strings>
<ABK_CALL comment="verb (command, button on screen to press to place a call);" controls="Button" products="HDX,VSX,CMAD,Venus Main">
<ARABIC notes="" last-change-date="" status="">打电话</ARABIC>
<CHINESE_S notes="" last-change-date="" status="">呼叫</CHINESE_S>
该代码的功能是:
从RPD的String中取出节点值,在CMAD的String中查找是否已经存在,如果存在,则返回CMAD中对应String的NodeName(节点名),并把两个节点名一个做节点名,一个做节点值写到xml文件中;如果不存在,则把RPD中的该节点写到另外一个xml文件中。代码如下:
import xml.dom.minidom
from xml.dom.minidom import Document
RPD_Str_path = "E:/PythonCode/StringResource.en-US.xaml"
RPD_dom = xml.dom.minidom.parse(RPD_Str_path)
CMAD_Str_path = "E:/PythonCode/M500_RPM13_0522.xml"
CMAD_dom = xml.dom.minidom.parse(CMAD_Str_path)
#得到根节点
RPD_root = RPD_dom.documentElement
CMAD_root = CMAD_dom.documentElement
def IsStr_already_Translated(RPD_Str):
for firstLevel in CMAD_root.childNodes:
for SecondLevel in firstLevel.childNodes:
if SecondLevel.nodeType == SecondLevel.ELEMENT_NODE:
if SecondLevel.nodeName == "ENGLISH_US":
if RPD_Str == SecondLevel.childNodes[0].data.strip():
return firstLevel.nodeName
else:
continue
else:
continue
else:
continue
else:
continue
else:
return "Null"
#用Document来写xml文件
Mapping_doc = Document()
Mapping_root = Mapping_doc.createElement("Common_String")
Mapping_doc.appendChild(Mapping_root)
Translation_doc = Document()
Translation_root = Translation_doc.createElement("Need_Translation_String")
Translation_doc.appendChild(Translation_root)
for node in RPD_root.childNodes:
if node.nodeType == node.ELEMENT_NODE:
# print node.getAttribute("x:Key") +" + "+ node.childNodes[0].data
CMAD_Key = IsStr_already_Translated(node.childNodes[0].data.strip())
if(CMAD_Key != "Null"):
mKey = Mapping_doc.createElement(node.getAttribute("x:Key"))
Mapping_root.appendChild(mKey)
mValue = Mapping_doc.createTextNode(CMAD_Key)
mKey.appendChild(mValue)
elif(CMAD_Key == "Null"):
Key = Translation_doc.createElement('sys:String')
Translation_root.appendChild(Key)
Key.setAttribute('x:Key', node.getAttribute("x:Key"))
Value = Translation_doc.createTextNode(node.childNodes[0].nodeValue)
Key.appendChild(Value)
continue
else:
path1 = "E:/PythonCode/ID_Mapping.xml"
try:
import codecs
f1 = codecs.open(path1, "wb", "utf-8")
f1.write(Mapping_doc.toprettyxml(indent=" "))
except:
print('Write xml file failed.... file:{0}'.format(path1))
path2 = "E:/PythonCode/Need_Translate_String.xml"
try:
f2 = codecs.open(path2, "wb", "utf-8")
f2.write(Translation_doc.toprettyxml(indent=" "))
except:
print('Write xml file failed.... file:{0}'.format(path2))
PS:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:
在线XML/JSON互相转换工具:
http://tools.jb51.net/code/xmljson
在线格式化XML/在线压缩XML:
http://tools.jb51.net/code/xmlformat
XML在线压缩/格式化工具:
http://tools.jb51.net/code/xml_format_compress
XML代码在线格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat
希望本文所述对大家Python程序设计有所帮助。
标签:python,xml文件
0
投稿
猜你喜欢
python查看自己安装的所有库并导出的命令
2022-03-01 03:03:08
Python求两个文本文件以行为单位的交集、并集与差集的方法
2021-12-25 09:12:18
图文详解OpenCV中光流以及视频特征点追踪
2022-12-30 01:45:08
python 基本数据类型占用内存空间大小的实例
2021-08-10 21:59:03
XML DOM介绍和例子
2007-10-15 20:23:00
Spring Data JPA的Audit功能审计数据库的变更
2024-01-21 18:30:29
Go语言计算两个经度和纬度之间距离的方法
2024-04-29 13:06:04
python数据挖掘Apriori算法实现关联分析
2022-06-02 12:29:15
Java连接Oracle数据库实例解析
2024-01-17 06:31:04
Win7的IIS7中ASP获得的系统日期格式为斜杠的解决办法
2012-12-04 19:57:33
python pycharm最新版本激活码(永久有效)附python安装教程
2022-08-16 14:08:56
利用ajax制作在线翻译聊天室
2007-12-28 21:44:00
如何使用FSO搜索硬盘文件
2007-09-27 12:59:00
Python深度学习之实现卷积神经网络
2021-08-11 01:57:01
Python 聊聊socket中的listen()参数(数字)到底代表什么
2022-10-17 00:49:25
Python通过Django实现用户注册和邮箱验证功能代码
2021-02-21 13:59:03
一文教会你用Python读取PDF文件
2021-03-23 00:24:55
python随机模块random使用方法详解
2022-07-02 04:31:10
python3实现名片管理系统
2022-01-21 22:11:01
win8下python3.4安装和环境配置图文教程
2022-10-29 03:23:29