Python论坛  - 讨论区

标题:[python-chinese] 如何遍历一个xml中的节点

2006年11月24日 星期五 14:59

bakefish yellowfool在gmail.com
星期五 十一月 24 14:59:35 HKT 2006



  
    aaa
    bbb
    ccc
    ddd
  

ÈçÉϵÄÒ»¸öxmlÎļþ£¬ÎÒÏÖÔÚÏë´ïµ½Á½¸öÄ¿µÄ£º
1¡¢¶Á³ö½ÚµãTestchildÏÂÃæËùÓеÄ×Ó½ÚµãÃû³Æ£¬ÓÃËüÀ´×öΪһ¸ödictionaryµÄkeyÖµ£¬È»ºó×Ó½ÚµãµÄÖµÒ²¶ÔÓ¦·ÅÔÚdictionaryÀï
2¡¢Ïë×öÒ»¸öÃüÁîÐеĵݽøÄ£ºýËÑË÷£¬±ÈÈçÎÒдÏ£¬Te Tes Bt£¬ÕâÀïҪʶ±ð³öÏëÕÒµÄÊÇÀïµÄÖµ

xmlÒ»µãÒ²²»Ê죬µ«ÊǸоõËüÓÃÀ´±£´æ²ÎÊý½á¹¹ºÍÃüÁî½á¹¹ºÜºÃ£¬¿ÉÊÇÎÒÏÖÔÚÎÞ·¨°´×Ô¼ºµÄÐÄÔ¸¶Á³ö£¬ÒòΪʱ¼äÌ«½ô£¬²éÁË°ëÌìûÕÒµ½£¬Ö»ºÃÇóÖú´ó¼ÒÁË£¬Ð»Ð»
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20061124/ea353930/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年11月24日 星期五 16:45

=?GB2312?B?u8bBiA==?= huangchonggnu在gmail.com
星期五 十一月 24 16:45:02 HKT 2006

libxml2 ÖÐÕë¶Ô½ÚµãÓÐget_Name(), get_content()·½·¨£¬¿ÉÒÔÈ¡µÃ½ÚµãÃûºÍ½ÚµãÖµ

ÔÚ06-11-24£¬bakefish <yellowfool在gmail.com> дµÀ£º
>
> 
> 
>   
>     aaa
>     bbb
>     ccc
>     ddd
>   
> 
> ÈçÉϵÄÒ»¸öxmlÎļþ£¬ÎÒÏÖÔÚÏë´ïµ½Á½¸öÄ¿µÄ£º
> 1¡¢¶Á³ö½ÚµãTestchildÏÂÃæËùÓеÄ×Ó½ÚµãÃû³Æ£¬ÓÃËüÀ´×öΪһ¸ödictionaryµÄkeyÖµ£¬È»ºó×Ó½ÚµãµÄÖµÒ²¶ÔÓ¦·ÅÔÚdictionaryÀï
> 2¡¢Ïë×öÒ»¸öÃüÁîÐеĵݽøÄ£ºýËÑË÷£¬±ÈÈçÎÒдÏ£¬Te Tes Bt£¬ÕâÀïҪʶ±ð³öÏëÕÒµÄÊÇÀïµÄÖµ
>
> xmlÒ»µãÒ²²»Ê죬µ«ÊǸоõËüÓÃÀ´±£´æ²ÎÊý½á¹¹ºÍÃüÁî½á¹¹ºÜºÃ£¬¿ÉÊÇÎÒÏÖÔÚÎÞ·¨°´×Ô¼ºµÄÐÄÔ¸¶Á³ö£¬ÒòΪʱ¼äÌ«½ô£¬²éÁË°ëÌìûÕÒµ½£¬Ö»ºÃÇóÖú´ó¼ÒÁË£¬Ð»Ð»
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20061124/f7d4d6f8/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年11月24日 星期五 17:05

zhang peng finalmdj在gmail.com
星期五 十一月 24 17:05:31 HKT 2006

import xml.dom.minidom
def main():
	str="""

  
    aaa
    bbb
    ccc
    ddd
  
""".encode("utf-8")
	dom = xml.dom.minidom.parseString(str)
	for x in dom.getElementsByTagName("Test"):
		.......

差不多是这个意思

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年11月26日 星期日 11:44

SP Z zsp007在gmail.com
星期日 十一月 26 11:44:08 HKT 2006

昨天写的一个类似程序

from xml.sax import handler, make_parser
import sys, string
class Lemmas:
    def __init__(self):
        #单词 解释 音标
        self.elemList=['']*3

    def key(self,key):
        self.__bind(0,key)

    def mean(self,mean):
        self.__bind(1,mean)

    def phonetic(self,phonetic):
        self.__bind(2,phonetic)

    def __bind(self,number,content):
        self.elemList[number]=content.strip().replace('"','""').encode('utf8')


    def isPhrase(self):
        if self.elemList[0].find(' ')!=-1:
            return True
        else:
            return False

    def __str__(self):
        if self.isPhrase():
            return '"'+string.join(self.elemList[:2],'","')+'"'
        else:
            return '"'+string.join(self.elemList,'","')+'"'




def outputManipulate(lemma):
    if lemma.isPhrase():
        file=phraseFile
    else:
        file=wordFile
    file.write(str(lemma)+'\n')
    #print str(lemma)+'\n'





class xdictHandler(handler.ContentHandler):
    def __init__(self):
        self.lemma=Lemmas()
        self.bufStr=""
    def startDocument(self):
        print "--------  Document Start --------"
    def endDocument(self):
        print "--------  Document End --------"
    def startElement(self, name, attrs):
        pass
    def endElement(self, name):

        if name=='br':
            self.bufStr+="
" return elif name=='key': self.lemma.key(self.bufStr) elif name=='phonetic': self.lemma.phonetic(self.bufStr) elif name=='lemma': self.lemma.mean(self.bufStr) outputManipulate(self.lemma) self.lemma=Lemmas() self.bufStr="" def characters(self, char): self.bufStr+=char if __name__ == '__main__': xmlPath=r"F:\job\word\devKit\xdict_utf8.xml" wordPath=r"F:\job\word\devKit\word.txt" phrasePath=r"F:\job\word\devKit\phrasePath.txt" wordFile=open(wordPath,'w') phraseFile=open(phrasePath,'w') # Create an instance of the Handler. handler = xdictHandler() # Create an instance of the parser. parser = make_parser() # Set the content handler. parser.setContentHandler(handler) inFile = open(xmlPath, 'r') # Start the parse. parser.parse(inFile) inFile.close() wordFile.close() phraseFile.close() from xml.sax import handler, make_parser import sys, string class Lemmas: def __init__(self): #单词 解释 音标 self.elemList=['']*3 def key(self,key): self.__bind(0,key) def mean(self,mean): self.__bind(1,mean) def phonetic(self,phonetic): self.__bind(2,phonetic) def __bind(self,number,content): self.elemList[number]=content.strip().replace('"','""').encode('utf8') def isPhrase(self): if self.elemList[0].find(' ')!=-1: return True else: return False def __str__(self): if self.isPhrase(): return '"'+string.join(self.elemList[:2],'","')+'"' else: return '"'+string.join(self.elemList,'","')+'"' #这个函数写的不好 def outputManipulate(lemma): if lemma.isPhrase(): file=phraseFile else: file=wordFile file.write(str(lemma)+'\n') #print str(lemma)+'\n' class xdictHandler(handler.ContentHandler): def __init__(self): self.lemma=Lemmas() self.bufStr="" def startDocument(self): print "-------- Document Start --------" def endDocument(self): print "-------- Document End --------" def startElement(self, name, attrs): pass def endElement(self, name): if name=='br': self.bufStr+="
" return elif name=='key': self.lemma.key(self.bufStr) elif name=='phonetic': self.lemma.phonetic(self.bufStr) elif name=='lemma': self.lemma.mean(self.bufStr) outputManipulate(self.lemma) self.lemma=Lemmas() self.bufStr="" def characters(self, char): self.bufStr+=char if __name__ == '__main__': xmlPath=r"F:\job\word\devKit\xdict_utf8.xml" wordPath=r"F:\job\word\devKit\word.txt" phrasePath=r"F:\job\word\devKit\phrasePath.txt" wordFile=open(wordPath,'w') phraseFile=open(phrasePath,'w') # Create an instance of the Handler. handler = xdictHandler() # Create an instance of the parser. parser = make_parser() # Set the content handler. parser.setContentHandler(handler) inFile = open(xmlPath, 'r') # Start the parse. parser.parse(inFile) inFile.close() wordFile.close() phraseFile.close() 2006/11/24, zhang peng <finalmdj at gmail.com>: > import xml.dom.minidom > def main(): > str=""" > > > aaa > bbb > ccc > ddd > > """.encode("utf-8") > dom = xml.dom.minidom.parseString(str) > for x in dom.getElementsByTagName("Test"): > ....... > > 差不多是这个意思 > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese -- I'm Earth-Man. 我是地球人. --zsp(张沈鹏)

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年11月26日 星期日 13:17

Zoom.Quiet zoom.quiet在gmail.com
星期日 十一月 26 13:17:29 HKT 2006

On 11/24/06, bakefish <yellowfool在gmail.com> wrote:
> 
> 
>   
>     aaa
>     bbb
>     ccc
>     ddd
>   
> 
> 如上的一个xml文件,我现在想达到两个目的:
import cElementTree
tree = cElementTree.XML(uxmlstring)
Te = {}
for i in tree.find("Testchild").getchildren():
    Te[i.tag]=i.text

相关iPython测试:
'''
In [112]: for i in e.find("Testchild").getchildren():
   .....:     Te[i.tag]=i.text
   .....:

In [113]: Te
Out[113]: {'Atest': 'aaa', 'Dtest': 'ddd', 'Ctest': 'ccc', 'Btest': 'bbb'}

In [114]: Te['Atest']
Out[114]: 'aaa'
'''

这样你就可以使用类似 Te['Atest'] 来获取值使用

更多参考:
http://effbot.org/zone/element.htm

> 1、读出节点Testchild下面所有的子节点名称,用它来做为一个dictionary的key值,然后子节点的值也对应放在dictionary里
> 2、想做一个命令行的递进模糊搜索,比如我写下,Te Tes
> Bt,这里要识别出想找的是里的值
>
> xml一点也不熟,但是感觉它用来保存参数结构和命令结构很好,可是我现在无法按自己的心愿读出,因为时间太紧,查了半天没找到,只好求助大家了,谢谢
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>


-- 
'''Time is unimportant, only life important!
blog@  http://blog.zoomquiet.org/pyblosxom/
wiki@    http://wiki.woodpecker.org.cn/moin/ZoomQuiet
douban@ http://www.douban.com/people/zoomq/
____________________________________
Please use OpenOffice.org to replace M$ office.
     http://zh.openoffice.org
Please use 7-zip to replace WinRAR/WinZip.
     http://7-zip.org/zh-cn/
You can get the truely Freedom from software.
'''

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号