Python XML grooming

Source: Internet
Author: User

Import ElementTree Module

Import Xml.etree.ElementTree as ET

To create an element instance, use the element constructor or the subelement () factory function.
Et. Element (): Typically used to create a root node
Et. Subelement (): Used to create child nodes

The ElementTree class can be used to wrap an element structure for conversion to and from XML.

Commonly used:
ElementTree traverse the entire document.
Element iterates over a single node or a child node is usually used.

element: Method and function tag = Noneattrib = Nonetext = Nonetail = None def append (self, sub Element): Def extend (self, elements): def insert (self, Index, subelement): Def remove (self, subelement): def G    Etchildren (self): def find (self, Path, namespaces=none): Def findtext (self, path, Default=none, Namespaces=none): def findall (self, Path, Namespaces=none): def-iterfind (self, Path, Namespaces=none): Def Clear (self): def get (SE    LF, key, Default=none): Def set (self, key, value): Def keys: def items (self): def iter (self, tag=none): def itertext (self): 
ElementTree: Common function    def getroot (self):     def parse (self, source, Parser=none):  #打开xml文件    def iter (self, Tag=none):    def getiterator (self, Tag=none): def-    find (self, Path, Namespaces=none):    def-FindText (self, Path, Default=none, Namespaces=none):    def findall (self, Path, Namespaces=none):    def iterfind (self, path, Namespaces=none):    def write (self, file_or_filename,              Encoding=none,              Xml_declaration=none,              Default_namespace=none,              method=none, *,              short_empty_elements=true):

First, parse (get to root node [Element])
1. STR mode:
node = ET. XML (Str_xml) = et.fromstring (str_xml) #获取到根节点 (Element)
#XML () =fromstring ()
2. File Method:
result = Et.parse ("File.xml") # Open file, (ElementTree)
root = Result.getroot () # gets to the root node, (Element)

Ii. modification (use of [Element] type Object)
tag, attrib, text, find, ITER, remove, set ...

Third, re-write the file

Write must use the ElementTree object to invoke the Write method operation.
1, str parsing method to write files

node = ET. XML (str_xml) #获取到根节点 ... Operation et = et. ElementTree (Root) #创建一个tree et.write ("File.xml", encoding= "Utf-8", Xml_declaration=true) #写入文件

2, file method write back file

result = Et.parse ("File.xml") #获取Tree root = Result.getroot () #获取到根节点 ... Modify Result.write ("File.xml", encoding= "Utf-8", xml_declaration=true) writeback file

Iv. Creating an XML file

Element #创建根节点
subelement # Creating child nodes
ElementTree # Creating a tree for writing to a file

Five, indentation

Import Minidom Module
From Xml.dom import Minidom
When writing to a file is not using the tree, use the following code (where root is element and node)

c = Minidom.parsestring (et.tostring (Root, encoding= "Utf-8")). Toprettyxml (indent= "\ T") F = open ("File.xml", "W", encoding= "Utf-8") F.write (c) f.close () 

For the above operations, you can define a function to use:

def wrap (root): a = et.tostring (root, encoding= "Utf-8") B = minidom.parsestring (a) c = b.toprettyxml (indent= "\ T") Return C

Vi. namespaces

1. Register the namespace:

Et.register_namespace ("com", "http://www.ehaomiao.com")

2. Call (join before the label needs to use the namespace, format {}) as follows:

School = ET. Element ("{http://www.ehaomiao.com}school") University = ET. Subelement (School, "{http://www.ehaomiao.com}university", attrib={"Time": "4"})

3. Results
The root node is displayed as follows: (one more sentence xmlns:com= "http://www.ehaomiao.com")
<com:school xmlns:com= "http://www.ehaomiao.com" >
The nodes in each call namespace are displayed as follows: (more than one COM: flag)
<com:university time= "4" >

Vii. Important

You can use the type () method to see if you encounter problems with object types during the operation.

Viii. creating an XML file exercise

#!/usr/bin/env python#-*-coding:utf-8-*-# @Time: 2017/12/1 0001 14:07# @Author: MingimportXml.etree.ElementTree as Etfrom xml.dom importMinidomet.register_namespace ("com", "http://www.ehaomiao.com") # Register namespaceSchool = ET. Element ("{http://www.ehaomiao.com}school") # Call Namespace University = ET. Subelement (School, "{http://www.ehaomiao.com}university", attrib={"Time": "4"}) D1 = ET. Subelement (University, "D1") # University is its father node D1.text = "Freshman"D2 = ET. Subelement (University, "D2") D2.text = "Sophomore"D3 = ET. Subelement (University, "D3") D3.text = "Juniors"D4 = ET. Subelement (University, "D4") D4.text = "Seniors"High_school = ET. Subelement (School, "{Http://www.ehaomiao.com}high_school", attrib={"Time": "3"}) G1 = ET. Subelement (High_school, "G1") G1.text = "Freshman"G2 = ET. Subelement (High_school, "G2") G2.text = "Sophomore" g3 = ET. Subelement (High_school, "G3" ) G3.text = "Senior"  Middle_school = ET. Subelement (School, "{Http://www.ehaomiao.com}middle_school", attrib={"Time": "3" }) C1 = ET. Subelement (Middle_school, "C1" ) C1.text = "The Junior"  C2 = ET. Subelement (Middle_school, "C2" ) C2.text = "The Junior"  C3 = ET. Subelement (Middle_school, "C3" ) C3.text = "The" # writes the file without indentation, writes to the file1.xml file et =  et. ElementTree (School) et.write ("File1.xml", encoding= "utf=8", xml_declaration=  True) def  Wrap (Root): "" " Add a newline character to all nodes of the XML file:p Aram root: Root node [element type]: return: Returns the string "" " a = et.tostring (root, encoding=" Utf-8 ") with indentation added  b =  minidom.parsestring (a) c = b.toprettyxml (indent= "\ T" ) return  C # write file has indentation, write to file2.xml file a =  Wrap (School) f = open ("File2.xml", "W", encoding= "Utf-8" ) F.write (a) f.close ()     
<?XML version= "1.0"?><Com:schoolxmlns:com= "Http://www.ehaomiao.com">    <com:university Time= "4">        <D1>Freshman</D1>        <D2>Sophomore</D2>        <D3>Junior</D3>        <D4>Senior</D4>    </com:university>    <Com:high_school Time= "3">        <G1>High</G1>        <G2>High school</G2>        <G3>Three</G3>    </Com:high_school>    <Com:middle_school Time= "3">        <C1>First</C1>        <C2>First</C2>        <C3>First</C3>    </Com:middle_school></Com:school>
File2.xml
<?XML version= ' 1.0 ' encoding= ' utf=8 '?><Com:schoolxmlns:com= "Http://www.ehaomiao.com"><com:university Time= "4"><D1>Freshman</D1><D2>Sophomore</D2><D3>Junior</D3><D4>Senior</D4></com:university><Com:high_school Time= "3"><G1>High</G1><G2>High school</G2><G3>Three</G3></Com:high_school><Com:middle_school Time= "3"><C1>First</C1><C2>First</C2><C3>First</C3></Com:middle_school></Com:school>
File1.xml

Python XML grooming

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.