国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python處理xml文件的方法小結

2020-02-16 01:28:12
字體:
來源:轉載
供稿:網友

本文實例講述了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.minidomfrom xml.dom.minidom import DocumentRPD_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.documentElementCMAD_root = CMAD_dom.documentElementdef 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)    continueelse:  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))            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阿巴嘎旗| 汉川市| 开原市| 民县| 巴林右旗| 平罗县| 即墨市| 香港| 墨玉县| 新龙县| 红安县| 祁门县| 轮台县| 清新县| 乌审旗| 靖州| 秦安县| 高安市| 临泉县| 德清县| 和龙市| 泰来县| 柞水县| 阿图什市| 孟州市| 南江县| 汕头市| 江达县| 安国市| 宜丰县| 丰都县| 横峰县| 嘉祥县| 阳高县| 旬阳县| 武夷山市| 甘孜| 古浪县| 石河子市| 特克斯县| 石河子市|