Summary of how python processes xml files and how python processes xml files

Source: Internet
Author: User

Summary of how python processes xml files and how python processes xml files

This document describes how to process xml files using python. We will share this with you for your reference. The details are as follows:

Some time ago, due to work needs, I learned a bit about how to process xml files using Python. Now I am posting it for your reference.

Xml files are superimposed on one layer of nodes, and the top layer is the root node. For example:

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

Here, sys: String is the node name, x: Key is the content of Attribute, and the xml node value is the child node of sys: String. It is a text node type. <Node name x: Key = "Attribute"> subnode...

Xml format of RPD:

<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 format:

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

TheFunctionYes:

Extract the node value from the String of RPD and check whether the node value already exists in the String of CMAD. If yes, return the corresponding String NodeName (node name) in CMAD ), the two nodes are named one as the node name, and one as the node value is written to the xml file. If the node does not exist, the node in RPD is written to another xml file. The Code is as follows:

Import xml. dom. minidomfrom xml. dom. minidom import Export entrpd_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) # obtain the root node RPD_root = RPD_dom.documentElementCMAD_root = CMAD_dom.documentElementdef IsStr_already_Translated (RPD_Str): for firstLevel in CMAD_root.c HildNodes: 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: return "Null" # Use Document to write the xml file Mapping_doc = Document () Mapping_root = Mapping_doc.createElement ("Common_Str Ing ") Mapping_doc.appendChild (Mapping_root) Translation_doc = Document () Translation_root = Translation_doc.createElement (" Need_Translation_String ") Translation_doc.appendChild (Translation_root) for node in parent: 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 = "") failed T: 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 = "") failed T: print ('write xml file failed .... file: {0 }'. format (path2 ))

PS: Here are some online tools for xml operations for your reference:

Online XML/JSON conversion tools:
Http://tools.jb51.net/code/xmljson

Online formatting XML/online compression XML:
Http://tools.jb51.net/code/xmlformat

XMLOnline compression/formatting tools:
Http://tools.jb51.net/code/xml_format_compress

XMLCode Online formatting and beautification tools:
Http://tools.jb51.net/code/xmlcodeformat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.