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.