🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: cn.bbs.comp.xml
Article #301

����Python��XML����(ת��)

#301
From: <Test/>
Date: Tue, 09 Feb 2010 14:33
216 lines
16890 bytes
David Mertz ������ �ɰ��� Python�ĵ�һ���ڶ����ָ������� Python ��ʹ��XML��Ȼ������Щ��������³��ֺ�Python �е� XML�������˺ܴ�ķ�չ�����ҵ��ǣ���Щ�Ľ��еĴ���������ݡ�������ر𲿷��У�������������ǰ��XML ���ߵ����ۣ����ṩ���µĴ���ʾ��
�� �������£�Python ��ʹ�� XML �ĵ����������ԡ��� Perl��REBOL��REXX �� TCL һ������һ�����Ľű����ԣ�������ǿ����ı��������������ң����˶Զ������͵��ı��ļ��������ļ��������⣬XML �ĵ�������������ӵ���ݽṹ��
�� ���� Python 2.0 �ж� XML ��֧��
�ı������г���ġ���ȡ���У�����������һЩ������ʽ�Ƚϡ���ʽͨ�����ܺܺõ��ʺ϶� XML ���г����﷨�����ʹ��?�Һã�Python������������������ȣ������д��?����ݽṹ��ֱ�ӷ�����ͨ��ʹ��������ԣ�������һϵ�� XML ��ص�ģ����԰����﷨�������������� XML��
XML-SIG ��ר����Ȥ�飩�ij�ԱΪά�� Python һϵ�� XML ����������๤���������� Python ר����Ȥ��һ��XML-SIG Ҫά���ʼ������б?�б?�������õIJο���ȫ���ĵ�����׼���������Դ������ı��ĺ�� �ο����ϣ���
�� Python 2.0 ��ʼ��Python �����׼���а��а�������� XML-SIG ��Ŀ�����µ� XML-SIG ����ܰ�һЩ Python ��׼���а���û�еġ������Ƚ����ԣ�����������������˵�Ŀ�� �� ���������е����� �� Python 2.0 �� XML ֧�ֽ��������Ȥ�ġ����˵��ǣ����� Python �汾�� xmllib �Ļ�֧���� Python 2.0+ �����˺ܴ��Ŀǰ��Python �û������ѡ�� DOM �� SAX �� expat ���������� XML ��ʹ�����������Ե� XML ������Ա������ʶ����Щ����
ģ �飺xmllib
xmllib ��һ������֤�ĵͼ��﷨��������Ӧ�ó���Աʹ�õ� xmllib ���Ը��� XMLParser �࣬���ṩ�����ĵ�Ԫ�أ����ض���������ǣ����ַ�ʵ�壩�ķ������� Python 1.5x �� Python 2.0+ ������ xmllib ��ʹ�÷�����û�仯���ھ���������¸�õ�ѡ����ʹ�� SAX ��������Ҳ�����������ļ����������ԺͿ�������˵��Ϊ��׼��
�����е� ʾ����ԭ��ר���е���ͬ������һ������ quotations.dtd �� DTD �Լ���� DTD ���ĵ� sample.xml ������� �ο����ϣ��Ի�ȡ�������ᵽ���ļ��ĵ����������µĴ�����ʾ�� sample.xml ��ÿ�����Ե�ǰ���У�������˷dz��򵥵�δ֪��Ǻ�ʵ��� ASCII ָʾ����������ı���Ϊ�����������?��ʹ�õ��κ��ۼ������ɳ���Ա���������е��ַ� (#PCDATA)�����������ı�ǵ��б��ʵ䣩��
�嵥 1: try_xmllib.pyimport
xmllib, string
class
QuotationParser
(xmllib.XMLParser):
"""Crude xmllib extractor for quotations.dtd document"""
__init__
(self):
xmllib.XMLParser.__init__(self)
self.thisquote = ''��������������������# quotation accumulator
def
handle_data
(self, data):
self.thisquote = self.thisquote + data
syntax_error
(self, message):
pass
def
start_quotations
(self, attrs):����������# top level tag
print
'--- Begin Document ---'
start_quotation
(self, attrs):
print
'QUOTATION:'
end_quotation
(self):
print
string.join(string.split(self.thisquote[:230]))+'...',
print
'('+str(len(self.thisquote))+' bytes)
'
self.thisquote = ''
unknown_starttag
(self, tag, attrs):
self.thisquote = self.thisquote + '{'
unknown_endtag
(self, tag):
self.thisquote = self.thisquote + '}'
unknown_charref
(self, ref):
self.thisquote = self.thisquote + '?'
unknown_entityref
(self, ref):
self.thisquote = self.thisquote + '#'
if
__name__ == '__main__':
parser = QuotationParser()
for
c
in
open("sample.xml").read():
parser.feed(c)
parser.close()
ģ�飺xmllib
xmllib ��һ������֤�ĵͼ��﷨��������Ӧ�ó���Աʹ�õ� xmllib ���Ը��� XMLParser �࣬���ṩ�����ĵ�Ԫ�أ����ض���������ǣ����ַ�ʵ�壩�ķ������� Python 1.5x �� Python 2.0+ ������ xmllib ��ʹ�÷�����û�仯���ھ���������¸�õ�ѡ����ʹ�� SAX ��������Ҳ�����������ļ����������ԺͿ�������˵��Ϊ��׼��
�����е� ʾ����ԭ��ר���е���ͬ������һ������ quotations.dtd �� DTD �Լ���� DTD ���ĵ� sample.xml ������� �ο����ϣ��Ի�ȡ�������ᵽ���ļ��ĵ����������µĴ�����ʾ�� sample.xml ��ÿ�����Ե�ǰ���У�������˷dz��򵥵�δ֪��Ǻ�ʵ��� ASCII ָʾ����������ı���Ϊ�����������?��ʹ�õ��κ��ۼ������ɳ���Ա���������е��ַ� (#PCDATA)�����������ı�ǵ��б��ʵ䣩��
�嵥 1: try_xmllib.pyimport
xmllib, string
class
QuotationParser
(xmllib.XMLParser):
"""Crude xmllib extractor for quotations.dtd document"""
__init__
(self):
xmllib.XMLParser.__init__(self)
self.thisquote = ''��������������������# quotation accumulator
def
handle_data
(self, data):
self.thisquote = self.thisquote + data
syntax_error
(self, message):
pass
def
start_quotations
(self, attrs):����������# top level tag
print
'--- Begin Document ---'
start_quotation
(self, attrs):
print
'QUOTATION:'
end_quotation
(self):
print
string.join(string.split(self.thisquote[:230]))+'...',
print
'('+str(len(self.thisquote))+' bytes)
'
self.thisquote = ''
unknown_starttag
(self, tag, attrs):
self.thisquote = self.thisquote + '{'
unknown_endtag
(self, tag):
self.thisquote = self.thisquote + '}'
unknown_charref
(self, ref):
self.thisquote = self.thisquote + '?'
unknown_entityref
(self, ref):
self.thisquote = self.thisquote + '#'
if
__name__ == '__main__':
parser = QuotationParser()
for
c
in
open("sample.xml").read():
parser.feed(c)
parser.close()
SAX ��XML �ļ� API���� XML �﷨�������Ĺ����﷨�������ӿڡ�������Ӧ�ó������߱�дʹ�� XML �﷨��������Ӧ�ó��򣬵�����ȴ��������ʹ�õ��﷨���������������� XML �� JDBC������Lars Marius Garshol��SAX for Python��
SAX �� ��ͬ���ṩ���﷨������ģ��� API �� ������һ�� XML �ĵ���˳��������ʹ����ķ����� xmllib ʾ�������ƣ�����ӳ���Ӧ�ó���Ա������һ�� handler �࣬�����﷨�������࣬�� handler ����ע�ᵽ�κ���ʹ�õ��﷨�������С����붨�� 4 �� SAX �ӿڣ�ÿ���ӿڶ��м�����������DocumentHandler��DTDHandler��EntityResolver �� ErrorHandler�������﷨��������DZ����ǣ�����������Ĭ�Ͻӿڡ���Щ����ִ���� xmllib ʾ����ͬ������
�嵥 3: try_sax.py
"Simple SAX example, updated for Python 2.0+"
import
string
import
xml.sax
from
xml.sax.handler
import
*
class
QuotationHandler
(ContentHandler):
"""Crude extractor for quotations.dtd compliant XML document"""
__init__
(self):
self.in_quote = 0
self.thisquote = ''
startDocument
(self):
print
'--- Begin Document ---'
startElement
(self, name, attrs):
if
name == 'quotation':
print
'QUOTATION:'
self.in_quote = 1
else:
self.thisquote = self.thisquote + '{'
endElement
(self, name):
if
name == 'quotation':
print
string.join(string.split(self.thisquote[:230]))+'...',
print
'('+str(len(self.thisquote))+' bytes)
'
self.thisquote = ''
self.in_quote = 0
else:
self.thisquote = self.thisquote + '}'
characters
(self, ch):
if
self.in_quote:
self.thisquote = self.thisquote + ch
if
__name__ == '__main__':
parser = xml.sax.make_parser()
handler = QuotationHandler()
parser.setContentHandler(handler)
parser.parse("sample.xml")
�� xmllib ��ȣ�����ʾ����Ҫע������С�£� .parse() ����������������ַ����Բ���Ϊ�﷨����������ѭ���� .parse() ͬ�������ؽ���һ���ļ���һ���ļ����󣬻����ڶ�����ļ�����һЩ���� .read() ��ʽ����
��DOM
DOM ��һ�� XML �ĵ��ĸ߼����ͱ�ʾ����ģ�Ͳ���ֻ��� Python������һ����ͨ XML ģ�ͣ������ �ο������Ի�ȡ��һ����Ϣ����Python �� DOM ���ǻ��� SAX �����ģ����Ұ����� Python 2.0 �ı�׼ XML ֧�������ƪ�����ޣ�û�н�����ʾ��ӵ������У����� XML-SIG �� ��Python/XML HOWTO�� �и����һ�����õ�����������
�� ������ģ��Ϊ XML �ĵ�ָ�������ͱ�ʾ�������ĵ�ʵ�������ĸ���ֻ��һ���Ӵ�����Ԫ��ʵ�����Ԫ���б�ʾ���ݺ���Ԫ�ص��ӽڵ㣬����Ҳ�������Ӵ��Դ����ơ������ �������������������������Ԫ�غ�����ֵ�������ɾ��ڵ㣬�Լ�����ת���� XML��DOM ���������޸� XML �ĵ�����Ϊ���Դ���һ�� DOM ����ͨ������½ڵ�������ƶ��������޸��������Ȼ�����һ���µ� XML �ĵ���Ϊ�������Ҳ�����Լ�����һ�� DOM ����Ȼ����ת���� XML�������ַ������ XML ����Ƚ��� <tag1>��</tag1> д���ļ��ķ�������
ʹ�� xml.dom ģ����﷨�����ڵ������������һЩ�䶯��Python 2.0 ���Դ�� DOM ʵ�ֱ���Ϊ xml.dom.minidom �����ṩ��������С�Ͱ汾�� DOM����Ȼ������� XML-SIG �� DOM ����Щ�����Ե����Բ�δ������ xml.dom.minidom �У�����Ҳ�����ע�⵽��һ�㡣
��� DOM ����ܼ򵥣�ֻ�裺
�嵥 4: �� XML �ļ��д��� Python DOM ����
from
xml.dom.minidom
import
parse, parseString
dom1 = parse('mydata.xml')��������# parse an XML file by name
ʹ�� DOM �������ַdz�ֱ�ӵ� OOP ģʽ�Ĺ�����Ȼ�������޷����̼���ֵIJ㼶������ѭ���о٣���������������嵥�����ԡ����磬������һ����ͨ�� DOM Python ����Ƭ�ϣ�
�� �� 5: ͨ�� Python DOM �ڵ����ĵ��
for
node
in
dom_node.childNodes:
if
node.nodeName == '#text':��������������# PCDATA is a kind of node,
PCDATA = node.nodeValue��������������# but not a new subtag
elif
node.nodeName == 'spam':
spam_node_list.append(node)��������# Create list of <spam> nodes
Python ��׼˵���ĵ�����һЩ����ϸ�� DOM ʾ���ҵ������������й�ʹ�� DOM �����ʾ������� �ο����ϣ�ָ���ķ�����Ȼ����ȷ�ģ��������·���������һЩ��������������Ը�ģ���������һ�� Python ��˵���ĵ���
ģ�飺 pyxie
pyxie ģ������ Python ��׼ XML ֧��֮�Ϲ����ģ���Ϊ XML �ĵ��ṩ�˸��ӵĸ߼��ӿڡ� pyxie ����������������� XML �ĵ�ת����һ�ָ����ڽ����﷨�����Ļ����еĸ�ʽ���������ṩ�˽� XML �ĵ������ɲ���������ķ����� pyxie ��ʹ�õĻ����е� PYX ��ʽ�Dz����������Ƶģ��乤�������ڼ������ԡ���֮���ĵ��� PYX ��ʾ���� XML ��ʾ��ȣ�������ʹ�ó���Ļ����е��ı����?�߽��д��?�� grep��sed��awk��bash��perl�����׼ python ģ�飬�� string �� re ����ݽ��� XML ת���� PYX ���ܽ�ʡ��๤����
pyxie �� XML �ĵ�����������ĸ����� DOM �е�˼·���ơ����� DOM ��׼�õ���������ԵĹ㷺֧�֣���ô��� XML �ĵ������ͱ�ʾ�DZ���ģ���������Ա��ʹ�� DOM ��׼��� pyxie ��
���ģ�飺 xml_pickle �� xml_objectify
�����п����˴��� XML �ĸ߼�ģ�飬��Ϊ xml_pickle �� xml_objectify ���һ�������ط�д���������ģ�飨����� �ο����ϣ����ڴ˲��������Ľ��ܡ����㡰�� Python ˼�������ǡ��� XML ˼����ʱ����Щģ��dz����á��ر��� xml_objectify ����Գ���Ա�����˼������е� XML ������ʹ���ڳ����г��ʹ�� Python ��ԭʼ������ʵ�ʵ� XML ��ݸ�ʽ����������ò��ɼ�ͬ�� xml_pickle ʹ Python ����Ա�ԡ�ԭʼ�� Python ����ʼ���ö������ݿ�����Դ���κ�Դ���룬Ȼ������ǣ�����أ����������û��Ժ������Ҫ�� XML ��ʽ��
--------------------------------------------------------------------------------
ת��: http://www.yanzhilu.com/blog/archives/3890
--
Happy XMLing


�� ��Դ:��ˮľ���� newsmth.net��[FROM: 114.249.215.*]
Technical Details
Message-ID: <4kL0B0$FiJ@newsmth.net>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news2.google.com!news.glorb.com!news.neu.edu.cn!news.newsmth.net!NEWSMTH

Show Full Headers
Complete Article Headers
X-Google-Thread: f7ebf7e2af,4fbd4d1ad03fb738,start
X-Google-Attributes: gidf7ebf7e2af,public,usenet
X-Google-NewGroupId: yes
X-Google-Language: Chinese,GB
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news2.google.com!news.glorb.com!news.neu.edu.cn!news.newsmth.net!NEWSMTH
From: thinkhy@newsmth.net-SPAM.no (<Test/>)
Newsgroups: cn.bbs.comp.xml
Subject: ����Python��XML����(ת��)
Date: 09 Feb 2010 14:33:49 GMT
Organization: ˮľ����
Message-ID: <4kL0B0$FiJ@newsmth.net>
Mime-Version: 1.0
Content-Type: text/plain; charset="gb2312"
Content-Transfer-Encoding: 8bit
X-Filename: XML/X/M.1265725968.i0