xml base (a)

xml is structured to transmit and store data.

element:

xml element from (and including) the start tag up to (and including) the end portion of the tag .

For example: This is an element of the following,

  book is the name of this element. category is an attribute of the element, followed with the content attribute, content attribute must be enclosed in double quotes.

  title is one element, but is a child element of the book. Harry Potter is the text content of this element (text)

<book category="CHILDREN">
        <title>Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
    </book>

 

xml must have one and only one root element, the example above book is the root element. The following title, author, etc. is sub-elements.

xml is case sensitive. book and Book is different.

So there is a corresponding in Python xml processing library, library xml following process with these sub-modules:

There are several different ideas to resolve the xml file:

ElementTree xml file is abstracted into a tree, it is a lightweight dom, convenient and friendly API. Code is good availability, speed and consume less memory.

dom is the XML document as a tree structure, is defined as nodes and leaves, through the operation of the tree to manipulate XML.

SAX event-driven model used by a number of triggers in the process of parsing XML in the event and call a user-defined callback functions to handle XML file.

 

 

So look at it this ElementTree. . . .

There are two classes:  ElementTree the entire XML document is represented as a tree  Element represents a single node in the tree. Interact with the entire document (read and write files) is usually  ElementTree completed level. Interactive single XML element and its children are in the  Element level of completion.

Let me talk about this Element,

Element(tagattrib={}**extra)

tag

This element identifies a string of what data (in other words, the element type, or called names) means.

attrib

Is a property of this element is optional, attributes used to define the dictionary format.

extra contains additional attributes, provided as keyword arguments

Create a new Element:

AS the ET xml.etree.ElementTree Import 
ELE = ET.Element (Tag) 
# add the attribute 
ele.attrib = { "attrib_name": Attrib} 
# add content 
ele.text = text

Here's how:

ele.clear () 

This function deletes all child elements, remove all attributes, and text and tail attributes set to None.

ele.get(keydefault=None)

Gets the element attribute named key returns the property value, if the property is not found, the default value is returned

ele.items ()

In (name, value) returns the form of a sequence of element attributes. Property returns in any order.

ele.keys ()

It returns the element attribute names in a list (list) of. Returns the name in any order.

ele.set(keyvalue)

The properties of the elements key to value. You can also use it to increase property

ele.append(subelement)

The element child element (the SubElement) to the end of the inner sub-element of the list of elements. If the child is not an element, then trigger the type of error.

ele.extend(subelements)

Additional sequence object elements with zero or more child elements. If the child is not an element, then trigger the type of error.

ele.find(matchnamespaces=None)

The first child to find a matching element. It may be a tag name match (tag) or path (path). Returns an element instance or not a return.

ele.findall(matchnamespaces=None)

Find all child elements matching the marking name or path. Return a document order contains a list of all matching elements

ele.findtext(matchdefault=Nonenamespaces=None)

Find text first matching child elements. Matching can be a tag name or path. Return the first matching text element, if the element is not found, the default value is returned.

ele.insert(indexsubelement)

In the element given sub-element insertion position. If the child is not an element, then trigger the type of error.

ele.iter(tag=None)

To create a current element is the root of the tree iterators. Iterator by document (depth-first) order traversal of this element and all elements below it. If the tag is not None, or '*', then the iterator returns only flag is equal to an element tag. If you modify a tree structure during the iterations, the result is undefined.

ele.iterfind(matchnamespaces=None)

Find all child elements matching the marking name or path. Returns an iterator to generate all of the elements in document order matching. Namespace prefix from the namespace mapping to an optional full name

him. itertext( )

Create a text iterators. Iterator document order traversal of this element and all child elements, and returns all internal text

remove(subelement)

Remove the child element from the element. And find * method, this method is based on the instance identifier (tag value or not based on the contents) are compared to the element

 

Guess you like

Origin www.cnblogs.com/ichbinhere/p/11742337.html