Chinaunix

标题: Python网络编程基础笔记-minidom解析XML文件 [打印本页]

作者: jcodeer    时间: 2007-11-05 21:59
标题: Python网络编程基础笔记-minidom解析XML文件
1.python解析XML文件
               
               
                """
解析XML文件
1.Element XML树的节点
2.Text代表文本,包括Element的换行符
3.scanNode为一递归函数,如果当前的节点有子节点,进行递归调用
4.Node的类型
    ELEMENT_NODE                = 1
    ATTRIBUTE_NODE              = 2
    TEXT_NODE                   = 3
    CDATA_SECTION_NODE          = 4
    ENTITY_REFERENCE_NODE       = 5
    ENTITY_NODE                 = 6
    PROCESSING_INSTRUCTION_NODE = 7
    COMMENT_NODE                = 8
    DOCUMENT_NODE               = 9
    DOCUMENT_TYPE_NODE          = 10
    DOCUMENT_FRAGMENT_NODE      = 11
    NOTATION_NODE               = 12
"""
from xml.dom import minidom,Node
Node.TEXT_NODE
def scanNode(node,level = 0):
    msg = node.__class__.__name__
    if node.nodeType == Node.ELEMENT_NODE:
        msg += ",tag" + node.tagName
    print " " * level * 4,msg
    if node.hasChildNodes:
        for child in node.childNodes:
            scanNode(child,level + 1)
doc = minidom.parse("JCSample.xml")
scanNode(doc)
2.测试使用的XML
?xml version="1.0" encoding="UTF-8"?>
book>
    title> Sample XML Thing /title>
    author>
        name>
            first>Benjamin/first>
            last>Smith/last>
        /name>
        affiliation>Springy Widgets,Inc./affiliation>
    /author>
   
    chapter number = "1">
        title>First chapter/title>
        para>
            I think widgets are great.you should buy lots
            of them from company>Springy widgets,Inc/company>
        /para>
    /chapter>
/book>


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/19742/showart_415181.html




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2