2012-12-04 170 views
9

我需要解析XML文件與一些CDATA塊的,我需要保留供以後繪製的xml:解析CDATA與蟒蛇

<process id="process1"> <log name="name1" device="device1"><![CDATA[timestamp value]]]></log> <log name="name2" device="device2"><![CDATA[timestamp value, timestamp value, timestamp]]]></log> </process>

我需要反覆並迅速做到這一點,我正在尋找最好的方法來做到這一點。我讀過ElementTree是更快的方法,但我接受其他建議。

+0

xtree是您的問題比元素樹更好的替代方案。 – Rajendra

回答

10

這裏是如何做到這一點的兩個例子:

from lxml import etree 
import xml.etree.ElementTree as ElementTree 

CONTENT = """ 
<process id="process1"> 
<log name="name1" device="device1"><![CDATA[timestamp value]]></log> 
<log name="name2" device="device2"><![CDATA[timestamp value, timestamp value, timestamp]]></log> 
</process> 
""" 

def parse_with_lxml(): 
    root = etree.fromstring(CONTENT) 
    for log in root.xpath("//log"): 
     print log.text 

def parse_with_stdlib(): 
    root = ElementTree.fromstring(CONTENT) 
    for log in root.iter('log'): 
     print log.text 

if __name__ == '__main__': 
    parse_with_lxml() 
    parse_with_stdlib() 

輸出:

timestamp value 
timestamp value, timestamp value, timestamp 
timestamp value 
timestamp value, timestamp value, timestamp 

text屬性它處理它在這兩種情況下。

+1

爲了表演,可以使用'cElementTree'(注:leadind'c') – jfs