Reading catalogues
- 1. What is XML? What are the characteristics?
- 2. Get tag properties
- 3. Get child Tags
- 4. Get label attribute values
- 5. Get data between labels pairs
- 6. Example
- 7. summary
Back to the top
1. What is XML? What are the characteristics?
xmlThat is,Extensible markup language,It can be used to mark up data and define data types. It is a source language that allows users to define their own markup language.
Example: del.xml


2. Get tag properties


1
2
3
4
5
6
7
8
9
10
11
12
|
'ATTRIBUTE_NODE' 'CDATA_SECTION_NODE' 'COMMENT_NODE' 'DOCUMENT_FRAGMENT_NODE' 'DOCUMENT_NODE' 'DOCUMENT_TYPE_NODE' 'ELEMENT_NODE' 'ENTITY_NODE' 'ENTITY_REFERENCE_NODE' 'NOTATION_NODE' 'PROCESSING_INSTRUCTION_NODE' 'TEXT_NODE' |
Running result
1
2
3
4
|
nodeName: catalog nodeValue: None nodeType: 1 ELEMENT_NODE: 1 |
Back to the top
3. Get child Tags


1
2
3
4
|
<class 'xml.dom.minicompat.NodeList' > [<DOM Element: maxid at 0 x 2707 a 48 >] maxid None |
Back to the top
4. Get label attribute values

print item2.getAttribute("id")

1
2
3
4
|
pytest 123456 4 2 |
Back to the top
5. Get data between labels pairs


1
2
|
Python test |
Back to the top
6. Example




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
C:\Users\jihite\Desktop\xml>python user.py user_nodes: [<DOM Element: user at 0 x 2758 c 48 >, <DOM Element: user at 0 x 2756288 >, <DOM Element: user at 0 x 2756888 >, <DOM Element: user at 0 x 2756 e 88 >, <DOM Elemen t: user at 0 x 275 e 4 c 8 >, <DOM Element: user at 0 x 275 eac 8 >] ----------------------------------------------------- No.: 1000001 name: Admin sex: boy age: 23 Email: admin@live.cn ----------------------------------------------------- No.: 1000002 name: Admin 2 sex: boy age: 22 Email: admin 2 @live.cn ----------------------------------------------------- No.: 1000003 name: Admin 3 sex: boy age: 27 Email: admin 3 @live.cn ----------------------------------------------------- No.: 1000004 name: Admin 4 sex: gril age: 25 Email: admin 4 @live.cn ----------------------------------------------------- No.: 1000005 name: Admin 5 sex: boy age: 20 Email: admin 5 @live.cn ----------------------------------------------------- No.: 1000006 name: Admin 6 sex: gril age: 23 Email: admin 6 @live.cn |
Back to the top
7. summary
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
minidom.parse(filename) Load read XML file doc.documentElement Getting XML document objects node.getAttribute(AttributeName) Get XML node attribute value node.getElementsByTagName(TagName) Get the collection of XML node objects node.childNodes #Returns a list of child nodes. node.childNodes[index].nodeValue Get XML node value node.firstChild #Access the first node. Equivalent to pagexml.childNodes[ 0 ] doc = minidom.parse(filename) doc.toxml( 'UTF-8' ) Returns the text represented by the XML of the Node node. Node.attributes[ "id" ] a.name #It's above. "id" a.value #Attribute value Accessing element properties |