Python parses xml file instance analysis and pythonxml instance analysis

Source: Internet
Author: User

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.

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.