2012-03-08 179 views
1

我有麻煩解析XML當它在形式:解析嵌套的XML與LXML和Python

<Cars> 
    <Car> 
     <Color>Blue</Color> 
     <Make>Ford</Make> 
     <Model>Mustant</Model> 
    </Car> 
    <Car> 
     <Color>Red</Color> 
     <Make>Chevy</Make> 
     <Model>Camaro</Model> 
    </Car> 
</Cars> 

我想通了如何解析1級的孩子是這樣的:

<Car> 
    <Color>Blue</Color> 
    <Make>Chevy</Make> 
    <Model>Camaro</Model> 
</Car> 

有了這樣的代碼:

from lxml import etree 
    a = os.path.join(localPath,file) 
    element = etree.parse(a) 
    cars = element.xpath('//Root/Foo/Bar/Car/node()[text()]') 
    parsedCars = [{field.tag: field.text for field in cars} for action in cars] 
    print parsedCars[0]['Make'] #Chevy 

我如何分析我們的多「車」的標籤是「汽車總動員」的子標籤?

回答

3

試試這個

from lxml import etree 
    a = os.path.join(localPath,file) 
    element = etree.parse(a) 
    cars = element.xpath('//Root/Foo/Bar/Car') 
    for car in cars: 
     colors = car.xpath('./Color') 
     makes = car.xpath('./Make') 
     models = car.xpath('./Model') 
+0

當我運行這段代碼找到顏色我得到的地址,而不是實際的對象。例如,當試圖查找顏色時,我得到[<元素顏色在0x2a9f0f8>] – lodkkx 2012-03-08 13:39:06

+0

它們返回元素對象。要獲得文本使用xpath''./Color/text()'' – Dikei 2012-03-08 13:45:33

+0

是的,我實際上已經想出了它 - 但使用'./Color/node()'來代替。兩者有什麼不同 - 他們都給我的文字。 – lodkkx 2012-03-08 13:47:32