Python XML Childnodes,childnodes[1].childnodes[0].data Example

Source: Internet
Author: User

Xml:

<?xml version= ' 1.0 ' encoding= ' utf-8 '?>
<!--This was a test about xml-->
<booklist type= ' scicence and enginerring ' >
<book category= ' math ' >
<title>learing math</title>
<title>learing math1</title>
<author>zhagn san</author>
<pageNumber>562</pageNumber>
</book>
<book category= ' python ' >
<title>learing python</title>
<author>li si</author>
<pageNumber>544</pageNumber>
</book>
</booklist>

The following code shows each level of node and content description:

#coding =utf-8

From Xml.dom.minidom Import Parse

Domtree=parse (r "D:\\test.xml")
Booklist=domtree.documentelement
Print Booklist
Print "*" *30

Books=booklist.getelementsbytagname (' book ')
Print "Books:", books
Print "Books[0].childnodes:", Books[0].childnodes
Print "books[0].childnodes[1]:", books[0].childnodes[1]
Print "Books[0].childnodes[1].childnodes:", Books[0].childnodes[1].childnodes
Print "books[0].childnodes[1].childnodes[0]:", Books[0].childnodes[1].childnodes[0]
Print "Books[0].childnodes[1].childnodes[0].data:", Books[0].childnodes[1].childnodes[0].data
#print "books[0].childnodes[1].childnodes[1]:", books[0].childnodes[1].childnodes[1]

Explain:

#books是获取booklist对象中所有book节点的list集合

Books: [<dom element:book at 0x28855d0>, <dom Element:book at 0x2885990>]

#books [0]. ChildNodes: is the first book node of all child nodes, is a list
Books[0].childnodes: [<dom text node "U ' \ n '"), <dom element:title at 0x28856e8>, <dom text Node "u ' \ n ', <dom element:title at 0x2885788>, <dom Text node "U ' \ n '";, <dom Element:author at 0x2885828>, <dom text node "U ' \ n '", <dom element:pagenumber at 0x28858c8>, <dom text node "U ' \ n '";

#books [0].childnodes[1]: Is the second child node of the first book node, is a title tag element-:<title>learing Math1</title> Child nodes that contain labels and labels: text nodes
BOOKS[0].CHILDNODES[1]: <dom element:title at 0x28856e8>

#books [0].childnodes[1].childnodes: Is a child node (text node) of the Second child node (the title tag Element) of the first book node, which is a list
Books[0].childnodes[1].childnodes: [<dom Text node "u ' learing ma ' ...";]

#Books[0].childnodes[1].childnodes[0]: The first element of the child node (text node-list) of the Second child node (title tag Element) of the first book node
Books[0].childnodes[1].childnodes[0]: <dom Text node "u ' learing ma ' ..." >

#Books[0].childnodes[1].childnodes[0].data: is the value of the first element of the second child node (title tag Element) of the first book node (text node-list)
Books[0].childnodes[1].childnodes[0].data:learing Math

#books [0].childnodes[1].childnodes[1]: The second element of the child node (text node-list) of the Second child node (the title tag Element) of the first book node is attempted, but the error is that there is only one element in the list
BOOKS[0].CHILDNODES[1].CHILDNODES[1]:
Traceback (most recent):
File "task_test.py", line +, in <module>
Print "books[0].childnodes[1].childnodes[1]:", books[0].childnodes[1].childnodes[1]
Indexerror:list index out of range

From this we know that

All. End with childnodes, the result is a list

The result is a node element that ends with a childnodes[i].

C:\python27\scripts>python task_test.py
<dom element:booklist at 0x28854b8>
******************************
books: [<dom element:book at 0x28855d0>, <dom Element:book at 0x2885990>]
books[0].childnodes: [<dom text node "U ' \ n '"), <dom element:title at 0x28856e8>, <dom Text nod E "U ' \ n '", <dom element:title at 0x2885788>, <dom Text node "U ' \ n '";, <dom Element:au Thor at 0x2885828>, <dom text node "U ' \ n '", <dom element:pagenumber at 0x28858c8>, <dom Text N Ode "U ' \ n '";
books[0].childnodes[1]: <dom element:title at 0x28856e8>
books[0].childnodes[1].childnodes: [<dom Text node "u ' learing ma ' ...";]
Books[0].childnodes[1].childnodes[0]: <dom Text node "u ' learing ma ' ..." >
books[0].childnodes[1].childnodes[0].data:learing Math
Books[0].childnodes[1].childnodes[1]:
Traceback (most recent):
File "task_test.py", line +, in <module>
print "books[0].childnodes[1].childnodes[1]:", books[0].childnodes[1].childnodes[1]
Indexerror:list Index out of range

Python XML Childnodes,childnodes[1].childnodes[0].data Example

Related Article

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.