Serial 45-XML parsing Python (and each sample using minidom etree)

First, we carry out a wave of presentation of XML reading

 

import xml.dom.minidom

# Responsible for parsing the package xml file

from xml.dom.minidom import parse


# Use minidom open xml file 

DOMTree = xml.dom.minidom.parse ( " D30_1_XmlNameSpace.xml " )

Print (DOMTree) # defined in the XML file as an object

# Get the document object 

DOC = DOMTree.documentElement # print out the names with the root object

print(doc)


# Display sub-elements

for ele in doc.childNodes:

    if ele.nodeName == "student:Name":

        print("=======Node:{0}=======".format(ele.nodeName))

        print(doc.childNodes)

    if ele.nodeName == "Age":

        Print (ele.getAttribute ( " JIO " )) # getting a property value of a node

 

We can also use this way to parse xml.DOM.etree

We offer methods:

(1) a tree structure to represent xml;

(2) root.getiterator: set to give the corresponding node may be iterative

(3)root.iter

(4) find (node_name): Find the specified node node_name returns a node

(5) root.findall (node_name): Returns the node of the plurality node_name

(6) node.tag: node corresponding tagename

(7) node.text: node text value

(8) node.attrib: a dictionary type of node attributes of content

 

i

mport xml.etree.ElementTree

root = xml.etree.ElementTree.parse("D30_1_XmlNameSpace.xml")

nodes = root.getiterator()

for node in nodes:

    print("{0}---{1}".format(node.tag,node.text))

print("===========================================")

ele_room_name = root.find("Location")

print(type(ele_room_name))

print("{0}----{1}".format(ele_room_name.tag,ele_room_name.text))

print("===========================================")

ele_room_name2 = root.findall ( " {HTTP: // my_room the Name} " ) # Here If "room: Name" is not parsed out

print(ele_room_name2)

for ele in ele_room_name2:

    print("{0}----{1}".format(ele.tag,ele.text))

ele_room_name2 = root.findall("room:Name")

print(ele_room_name2)

for ele in ele_room_name2:

    print("{0}----{1}".format(ele.tag,ele.text))

Third, the source

D30_2_XmlAnalysis.py

https://github.com/ruigege66/Python_learning/blob/master/D30_2_XmlAnalysis.py

2.CSDN: https: //blog.csdn.net/weixin_44630050 (Xi Jun Jun Moods do not know - Rui)

3. Park blog: https: //www.cnblogs.com/ruigege0000/

4. Welcomes the focus on micro-channel public number: Fourier transform public personal number, only for learning exchanges, backstage reply "gifts" to get big data learning materials

 

 

Guess you like

Origin www.cnblogs.com/ruigege0000/p/11741579.html