本文共 5584 字,大约阅读时间需要 18 分钟。
Python XML parser provides us an easy way to read the XML file and extract useful data. Today we will look into python ElementTree XML API and learn how to use it to parse XML file as well as modify and create XML documents.
Python XML解析器为我们提供了一种读取XML文件并提取有用数据的简便方法。 今天,我们将研究python ElementTree XML API,并学习如何使用它来解析XML文件以及修改和创建XML文档。
Let’s get started with Python XML parser examples using ElementTree.
让我们开始使用ElementTree的Python XML解析器示例。
We will start with a very simple example to create an XML file programmatically and then, we will move to more complex files.
我们将从一个非常简单的示例开始,以编程方式创建XML文件,然后,我们将转向更复杂的文件。
In this example, we will create a new XML file with an element and a sub-element. Let’s get started straight away:
在此示例中,我们将创建一个带有一个元素和一个子元素的新XML文件。 让我们立即开始:
import xml.etree.ElementTree as xmldef createXML(filename): # Start with the root element root = xml.Element("users") children1 = xml.Element("user") root.append(children1) tree = xml.ElementTree(root) with open(filename, "wb") as fh: tree.write(fh)if __name__ == "__main__": createXML("test.xml")
Once we run this script, a new file will be created in the same directory with file named as test.xml
with following contents:
一旦运行此脚本,将在同一目录中创建一个新文件,名为test.xml
文件具有以下内容:
There are two things to notice here:
这里有两件事要注意:
wb
mode instead of w
as we need to in binary mode. 写入文件时,我们使用wb
模式而不是w
因为我们需要以二进制模式 。 Let’s improve the program by adding values to the XML elements:
让我们通过向XML元素添加值来改进程序:
import xml.etree.ElementTree as xmldef createXML(filename): # Start with the root element root = xml.Element("users") children1 = xml.Element("user") root.append(children1) userId1 = xml.SubElement(children1, "id") userId1.text = "123" userName1 = xml.SubElement(children1, "name") userName1.text = "Shubham" tree = xml.ElementTree(root) with open(filename, "wb") as fh: tree.write(fh)if __name__ == "__main__": createXML("test.xml")
Once we run this script, we will see that new elements are added added with values. Here are the content of the file:
运行此脚本后,我们将看到添加了新元素并添加了值。 这是文件的内容:
123 Shubham
This is perfectly valid XML and all tags are closed. Please note that I formatted the XML myself as the API writes the complete XML in a single fine which is kind a bit, incompleteness!
这是完全有效的XML,并且所有标签均已关闭。 请注意,我自己格式化了XML,因为API一口气写出了完整的XML,这有点不完整!
Now, let’s start with editing files.
现在,让我们开始编辑文件。
We will use the same XML file we showed above. We just added some more data into it as:
我们将使用上面显示的相同XML文件。 我们只是向其中添加了更多数据:
123 Shubham 0 234 Pankaj 0 345 JournalDev 0
Let’s try and update salaries of each user:
让我们尝试更新每个用户的薪水:
import xml.etree.ElementTree as xmldef updateXML(filename): # Start with the root element tree = xml.ElementTree(file=filename) root = tree.getroot() for salary in root.iter("salary"): salary.text = '1000' tree = xml.ElementTree(root) with open("updated_test.xml", "wb") as fh: tree.write(fh)if __name__ == "__main__": updateXML("test.xml")
It is worth noticing that if you try to update an elements value to an integer, it won’t work. You will have to assign a String, like:
值得注意的是,如果您尝试将元素值更新为整数,将无法使用。 您将必须分配一个字符串,例如:
salary.text = '1000'
instead of doing:
而不是做:
salary.text = 1000
This time, let’s try to parse the XML data present in the file and print the data:
这次,让我们尝试解析文件中存在的XML数据并打印数据:
import xml.etree.cElementTree as xml def parseXML(file_name): # Parse XML with ElementTree tree = xml.ElementTree(file=file_name) print(tree.getroot()) root = tree.getroot() print("tag=%s, attrib=%s" % (root.tag, root.attrib)) # get the information via the children! print("-" * 40) print("Iterating using getchildren()") print("-" * 40) users = root.getchildren() for user in users: user_children = user.getchildren() for user_child in user_children: print("%s=%s" % (user_child.tag, user_child.text)) if __name__ == "__main__": parseXML("test.xml")
When we run above script, below image shows the output produced.
当我们在脚本上方运行时,下图显示了生成的输出。
In this post, we studied how to extract, parse, and transform XML files. ElementTree
is one of the most efficient APIs to do these tasks. I would suggest you try some more examples of XML parsing and modifying different values in XML files.
在本文中,我们研究了如何提取,解析和转换XML文件。 ElementTree
是执行这些任务的最高效的API之一。 我建议您尝试更多XML解析和修改XML文件中不同值的示例。
Reference:
参考:
翻译自:
转载地址:http://bymzd.baihongyu.com/