Python's XML

Source: Internet
Author: User

Note : Python has several ways of parsing XML, and the following "ET" represents the Library "ElementTree" (Import Xml.etree.ElementTree as ET)

1. Et_tree = Et.parse (' Xxx.xml '): The parameter is a stream object or file name that returns an entire XML file object.

2. Et_root = Et_tree.getroot (): Gets the root node, the following is the node usage as an example of the root node et_root.

> Len (et_root): Number of direct child elements;

> For Child_node in Et_root: Iterates through the nodes of each of the immediate child elements;

> Et_root.attrib: Returns a dictionary of the node's property names and their values;

> Et_root[3]: Returns the immediate child element node of the specified index;

> Et_root.find (node_name): Finds the first matching immediate child element node under a node, and returns none if there is no child node or if the node does not have a child node at all;

> Et_root.findall (node_name): Finds all the immediate child element nodes under the node, if the node name is preceded by a double slash, it means to find all the child elements under the node, not just the immediate child elements, such as: Et_root.findall ('/// Author '), which indicates that all child nodes named "Author" are found under the Et_root node;

> Et_root.text: Returns the text content of the node (if any);

3.

Python's XML

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.