• notice
  • Congratulations on the launch of the Sought Tech site

python how to implement modify the contents of xml files

This article mainly explains "how python can modify the content of xml files". The content of the explanation in the article is simple and clear, easy to learn and understand. Please follow the ideas of the editor to study and learn together "how to modify python" xml file content"!

XML was designed to transmit and store data.

HTML is designed to display data.

XML stands for eXtensible Markup Language.

Extensible Markup Language (English: Extensible Markup Language, referred to as: XML) is a markup language that is simplified and modified from the Standard Generalized Markup Language (SGML). It mainly uses Extensible Markup Language, Extensible Style Language (XSL), XBRL and XPath.

Directly on the code, use it.

First, you need to prepare a test xmlfile, my file name is text.xml;

< data >
< country name = "Liechtenstein" >
< rank > yunweijia </ rank >
< year > 2022 </ year >
<gdppc>141100</gdppc>
< neighbor name = "Austria" direction = "E" />
< neighbor name = "Switzerland" direction = "W" />
</ country >
< country name = "Singapore" >
< rank > yunweijia </ rank >
< year > 2023 </ year >
<gdppc>59900</gdppc>
< neighbor name = "Malaysia" direction = "N" />
</ country >
< country name = "Panama" >
< rank >yunweijia </ rank >
< year > 2024 </ year >
<gdppc>13600</gdppc>
< neighbor name = "Costa Rica" direction = "W" />
< neighbor name = "Colombia" direction = "E" />
</ country >
</data>

Then use the following code to modify;

import xml.etree.ElementTree as ET
  def change_one_xml (xml_path, xml_dw, update_content) :
# Open the xml document
doc = ET.parse(xml_path)
root = doc.getroot()
# Find and modify road strength
sub1 = root.find(xml_dw)
# Modify label content
sub1.text = update_content
# Save changes
doc.write(xml_path)

# To modify the file
xml_path = r'test.xml'

# Modify the xpath location in the file
xml_dw = './/country[@name="Singapore"]/year'

# What content do you want to modify
update_content = '9999'
change_one_xml(xml_path, xml_dw, update_content)

After running, we can see that the content of the source file becomes;

< data >
< country name = "Liechtenstein" >
< rank > yunweijia </ rank >
< year > 2022 </ year >
<gdppc>141100</gdppc>
< neighbor name = "Austria" direction = "E" />
< neighbor name = "Switzerland" direction = "W" />
</ country >
< country name = "Singapore" >
< rank > yunweijia </ rank >
< year > 9999 </ year >
<gdppc>59900</gdppc>
< neighbor name = "Malaysia" direction = "N" />
</ country >
< country name = "Panama" >
< rank >yunweijia </ rank >
< year > 2024 </ year >
<gdppc>13600</gdppc>
< neighbor name = "Costa Rica" direction = "W" />
< neighbor name = "Colombia" direction = "E" />
</ country >
</data>

Thank you for reading. The above is the content of "How does python realize the modification of the content of the xml file". After the study of this article, I believe that everyone has a deeper understanding of how python realizes the modification of the content of the xml file. 


Tags

Technical otaku

Sought technology together

Related Topic

0 Comments

Leave a Reply

+