Python parses xml file instance analysis and pythonxml instance analysis
This document describes how to parse xml files using python. Share it with you for your reference. The details are as follows:
It is very convenient for python to parse xml. It is also explained in dive into python.
If the xml structure is as follows:
<?xml version="1.0" encoding="utf-8"?> <books> <book> <author>zoer</author> <title>think in java</title> <content>this is a good book</content> </book> <book> <author>naughty</author> <title>gone with the wind</title> <content>this is a good book 2</content> </book> <book> <author>cc</author> <content>this is a good book 3</content> </book> </books>
The third book is not marked with a title. Because you do not need to trust the code input, you must check the Code (for example, check whether there are any sub-tags ).
The parsing code is as follows:
# Coding = UTF-8 # parse all books # author: naughty610 # date: 2012-8-16 import xml. dom. minidom dom = xml. dom. minidom. parse ('C:/Users/naughty/Desktop/books. xml ') root = dom.doc umentElement # obtain each next-layer node for node in root. childNodes: # in this way, the nodes at the lower layer of the root node are obtained, instead of all nodes below the root node # all non-text nodes if node. nodeType = node. ELEMENT_NODE: # Use the author field author = node. getElementsByTagName ("author") if len (author)> = 1: print author [0]. childNodes [0]. data # obtain the title field title = node. getElementsByTagName ("title") if len (title)> = 1: print title [0]. childNodes [0]. data # retrieve content field content = node. getElementsByTagName ("content") if len (content)> = 1: print content [0]. childNodes [0]. data print "........................ parting line ........................"
I hope this article will help you with Python programming.