Summary of how python handles xml files

  • 2020-05-30 20:30:25
  • OfStack

This example shows how python handles xml files. I will share it with you for your reference as follows:

Due to the need of work, I have learned 1 point of handling xml files with Python. Now I will post it for your reference.

The xml file is stacked by node layer 1, layer 1, and the top layer is the root node. Such as:

<sys:String x:Key="STR_License_WithoutLicense">Sorry, you are not authorized.</sys:String>

Where sys: String is the node name, Attribute is the content of x:Key, and xml node value is the child node of sys: String, which is the text node type. < Node name x:Key="Attribute" > Child node...

RPD xml format:

<sys:String x:Key="STR_Startup_LaunchRPD">Launching Polycom RealPresence Desktop</sys:String>
<sys:String x:Key="STR_Startup_CheckFolder">Checking folder</sys:String>

xml format for CMAD:

 <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=""> Make a phone call </ARABIC>
  <CHINESE_S notes="" last-change-date="" status=""> call </CHINESE_S>

The function of this code is:

Take the node value from RPD's String, check whether it already exists in CMAD's String, if so, return CMAD's NodeName (node name) corresponding to String in CMAD, and write one of the two node names as the node name and one as the node value into xml file; If it does not exist, the node in RPD is written to another xml file. The code is as follows:

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)
# I get the root node 
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
    return "Null"
# with Document To write xml file 
Mapping_doc = Document()
Mapping_root = Mapping_doc.createElement("Common_String")
Translation_doc = Document()
Translation_root = Translation_doc.createElement("Need_Translation_String")
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"))
    mValue = Mapping_doc.createTextNode(CMAD_Key)
  elif(CMAD_Key == "Null"):
    Key = Translation_doc.createElement('sys:String')
    Key.setAttribute('x:Key', node.getAttribute("x:Key"))
    Value = Translation_doc.createTextNode(node.childNodes[0].nodeValue)
  path1 = "E:/PythonCode/ID_Mapping.xml"
    import codecs
    f1 =, "wb", "utf-8")
    f1.write(Mapping_doc.toprettyxml(indent=" "))
    print('Write xml file failed.... file:{0}'.format(path1))
  path2 = "E:/PythonCode/Need_Translate_String.xml"
    f2 =, "wb", "utf-8")
    f2.write(Translation_doc.toprettyxml(indent=" "))
    print('Write xml file failed.... file:{0}'.format(path2))

PS: here are some other online tools about xml operation for your reference:

Online XML/JSON interconversion tool:

Online formatting XML/ online compression XML:

XML online compression/formatting tools:

XML code online formatting beautification tool:

More about Python related topics: interested readers to view this site "Python xml data operation skill summary", "Python data structure and algorithm tutorial", "Python Socket programming skills summary", "Python function using techniques", "Python string skills summary", "Python introduction and advanced tutorial" and "Python file and directory skills summary"

I hope this article is helpful for you to design Python program.

Related articles: