<?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>
#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.documentElement
#获取每一个下一层节点
for node in root.childNodes:
#这样取得的是root节点以下一层的节点,而不是root节点以下所有节点
#取所有非text节点
if node.nodeType == node.ELEMENT_NODE:
#取author字段
author=node.getElementsByTagName("author")
if len(author)>=1:
print author[0].childNodes[0].data
#取title字段
title=node.getElementsByTagName("title")
if len(title)>=1:
print title[0].childNodes[0].data
#取content字段
content=node.getElementsByTagName("content")
if len(content)>=1:
print content[0].childNodes[0].data
print "........................parting line........................"
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有