How to modify content using DOM in XML
Apr 02, 2025 pm 06:42 PMHow to modify an XML document using DOM in Python? Use minidom to parse XML files as DOM trees. Get the target node to be modified. Use the firstChild property to modify the node text content. Write to the modified XML file. Free up memory to avoid leakage.
XML DOM modification: In-depth analysis and practical skills
Have you ever thought about how to efficiently modify the content of an XML document? Directly modifying XML files with a text editor is not only time-consuming and labor-intensive, but also prone to errors, resulting in the failure of the XML structure. At this time, the DOM (Document Object Model) comes in handy. This article will explore in-depth how to use DOM to modify XML content, and share some problems and solutions encountered in actual applications. After reading this article, you will master the essence of DOM modifying XML and be able to write efficient and robust code.
First, we need to clarify the essence of DOM: it parses the XML document into a tree structure, which makes it easier for us to access and operate each node programmatically. This is like dismantling a big tree into branches and leaves, and we can modify, add or delete these components at will. Python's xml.dom.minidom
module provides such capabilities.
Let's look at a simple example, suppose we want to modify a simple XML file:
<code class="xml"><bookstore> <book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="children"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> </bookstore></code>
We hope to change the price of Everyday Italian
to 35.00
. Using minidom
, the code is as follows:
<code class="python">from xml.dom.minidom import parse dom = parse("bookstore.xml") # 解析XML文件root = dom.documentElement # 獲取根節(jié)點books = root.getElementsByTagName("book") # 獲取所有book節(jié)點for book in books: if book.getElementsByTagName("title")[0].firstChild.data == "Everyday Italian": price_node = book.getElementsByTagName("price")[0] price_node.firstChild.data = "35.00" break # 找到目標(biāo)節(jié)點后退出循環(huán),提高效率with open("bookstore_modified.xml", "w") as f: dom.writexml(f, addindent=" ", newl="\n", encoding="utf-8") # 寫入修改后的XML文件dom.unlink() # 釋放內(nèi)存,非常重要!</code>
This code clearly shows the process of DOM modifying XML: first parsing the XML file, then finding the target node (price node), modifying its text content, and finally writing the modified XML file. Pay attention to the last line dom.unlink()
. This step is crucial. It frees up the memory occupied by the DOM tree to avoid memory leakage, especially when dealing with large XML files.
In advanced usage, you may encounter situations where you need to add or delete nodes. appendChild()
and removeChild()
methods are used to add and delete child nodes respectively. Remember that DOM operations are based on tree structures, and you need to clarify the parent-child relationship between nodes in order to correctly add or delete operations.
Problems that may be encountered: When dealing with large XML files, the DOM can consume a lot of memory. For super-large XML files, the SAX (Simple API for XML) parser is a better choice because it is an event-based parsing method with a lower memory footprint. However, SAX's programming model is more complex than DOM. Which parser to choose depends on your specific requirements and the size of the XML file.
In terms of performance optimization, try to minimize the number of traversals on the DOM tree. Rationally utilizing XPath expressions can improve the efficiency of finding target nodes. In addition, good programming habits, such as using meaningful variable names and adding necessary comments, can improve the readability and maintainability of the code. Remember, concise and efficient code is the pursuit of programmers.
The above is the detailed content of How to modify content using DOM in XML. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Yes,aPythonclasscanhavemultipleconstructorsthroughalternativetechniques.1.Usedefaultargumentsinthe__init__methodtoallowflexibleinitializationwithvaryingnumbersofparameters.2.Defineclassmethodsasalternativeconstructorsforclearerandscalableobjectcreati

Frequently asked questions about IIS servers after Windows update can be solved through the following steps: 1. If the IIS service cannot be started, check the service status and event log, try to restart the service or re-register/install IIS; 2. When the application pool crashes abnormally, check the application log, confirm the .NET version and permission settings, try to reset the identity or use the built-in account; 3. When the website has HTTP500 errors or blank pages, enable detailed error information, check the module configuration, and test the location problem through local browsing and simple pages; 4. When SSL binding or certificate fails, verify the binding configuration, certificate trust and private key permissions, detect port conflicts, and use tools to test the SSL connection, and rebind the certificate or update the root certificate if necessary.

In Python, using a for loop with the range() function is a common way to control the number of loops. 1. Use when you know the number of loops or need to access elements by index; 2. Range(stop) from 0 to stop-1, range(start,stop) from start to stop-1, range(start,stop) adds step size; 3. Note that range does not contain the end value, and returns iterable objects instead of lists in Python 3; 4. You can convert to a list through list(range()), and use negative step size in reverse order.

Python's onelineifelse is a ternary operator, written as xifconditionelsey, which is used to simplify simple conditional judgment. It can be used for variable assignment, such as status="adult"ifage>=18else"minor"; it can also be used to directly return results in functions, such as defget_status(age):return"adult"ifage>=18else"minor"; although nested use is supported, such as result="A"i

The key to using Python to call WebAPI to obtain data is to master the basic processes and common tools. 1. Using requests to initiate HTTP requests is the most direct way. Use the get method to obtain the response and use json() to parse the data; 2. For APIs that need authentication, you can add tokens or keys through headers; 3. You need to check the response status code, it is recommended to use response.raise_for_status() to automatically handle exceptions; 4. Facing the paging interface, you can request different pages in turn and add delays to avoid frequency limitations; 5. When processing the returned JSON data, you need to extract information according to the structure, and complex data can be converted to Data

appcmd.exe is a command line tool that comes with IIS7 and above, which can be used to efficiently manage IIS. 1. Can be used to manage sites and applications, such as starting and stopping sites (such as appcmdstopsite/site.name:"MySite"), list running sites, and add or delete applications. 2. Configurable application pools, including creating (appcmdaddapppool/name:MyAppPool), setting .NETCLR version (appcmdsetapppool/apppool.name:MyAppPool/managedRuntimeVersion:v4

Reading JSON files can be implemented in Python through the json module. The specific steps are: use the open() function to open the file, use json.load() to load the content, and the data will be returned in a dictionary or list form; if you process JSON strings, you should use json.loads(). Common problems include file path errors, incorrect JSON format, encoding problems and data type conversion differences. Pay attention to path accuracy, format legality, encoding settings, and mapping of boolean values and null.

Python's map() function implements efficient data conversion by acting as specified functions on each element of the iterable object in turn. 1. Its basic usage is map(function,iterable), which returns a "lazy load" map object, which is often converted to list() to view results; 2. It is often used with lambda, which is suitable for simple logic, such as converting strings to uppercase; 3. It can be passed in multiple iterable objects, provided that the number of function parameters matches, such as calculating the discounted price and discount; 4. Usage techniques include combining built-in functions to quickly type conversion, handling None situations similar to zip(), and avoiding excessive nesting to affect readability. Mastering map() can make the code more concise and professional
