Python XML and pythonxml

Source: Internet
Author: User

Python XML and pythonxml

Note: Python supports multiple XML parsing methods. The following "ET" indicates 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, and the object of the entire xml file is returned.

2. et_root = et_tree.getroot (): Get the root node. The following uses the root node et_root as an example.

> Len (et_root): number of direct sub-elements;

> For child_node in et_root: traverses each direct child element node under the node;

> Et_root.attrib: a dictionary consisting of the attribute names and values of the returned nodes;

> Et_root [3]: returns the direct sub-element node of the specified index;

> Et_root.find (node_name): finds the first matched direct child element node under the node. If the child node does not exist or the node does not have a child node, None is returned;

> Et_root.findall (node_name): searches for all direct child element nodes under a node. If a double slash is added before the node name, all child elements under the node are searched, instead of just sub-elements, such as et_root.findall ('// author'), it is used to find all the sub-nodes named "author" under the et_root node;

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

3.

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.