1
我有下面的XML:解析XML與LXML和Python
<nfl>
<season season="2012"/>
<conference label="AFC">
<division label="Eastern Division">
<team city="Buffalo" name="Bills" alias="Buf" />
<team city="Miami" name="Dolphins" alias="Mia" />
<team city="New England" name="Patriots" alias="NE" />
<team city="New York" name="Jets" alias="NYJ" />
</division>
<division label="Western Division">
<team city="Denver" name="Broncos" alias="Den" />
<team city="Kansas City" name="Chiefs" alias="KC" />
<team city="Oakland" name="Raiders" alias="Oak" />
<team city="San Diego" name="Chargers" alias="SD" />
</division>
<division label="Northern Division">
<team city="Cincinnati" name="Bengals" alias="Cin" />
<team city="Cleveland" name="Browns" alias="Cle" />
<team city="Pittsburgh" name="Steelers" alias="Pit" />
<team city="Baltimore" name="Ravens" alias="Bal" />
</division>
<division label="Southern Division">
<team city="Houston" name="Texans" alias="Hou" />
<team city="Tennessee" name="Titans" alias="Ten" />
<team city="Indianapolis" name="Colts" alias="Ind" />
<team city="Jacksonville" name="Jaguars" alias="Jac" />
</division>
</conference>
<conference label="NFC">
<division label="Eastern Division">
<team city="Dallas" name="Cowboys" alias="Dal" />
<team city="New York" name="Giants" alias="NYG" />
<team city="Philadelphia" name="Eagles" alias="Phi" />
<team city="Washington" name="Redskins" alias="Was" />
</division>
<division label="Western Division">
<team city="St. Louis" name="Rams" alias="StL" />
<team city="Arizona" name="Cardinals" alias="Ari" />
<team city="San Francisco" name="49ers" alias="SF" />
<team city="Seattle" name="Seahawks" alias="Sea" />
</division>
<division label="Northern Division">
<team city="Chicago" name="Bears" alias="Chi" />
<team city="Detroit" name="Lions" alias="Det" />
<team city="Green Bay" name="Packers" alias="GB" />
<team city="Minnesota" name="Vikings" alias="Min" />
</division>
<division label="Southern Division">
<team city="Atlanta" name="Falcons" alias="Atl" />
<team city="New Orleans" name="Saints" alias="NO" />
<team city="Tampa Bay" name="Buccaneers" alias="TB" />
<team city="Carolina" name="Panthers" alias="Car" />
</division>
</conference>
</nfl>
我要加載到我的模型,團隊「城市」,「名」和「別名」與父母一起「師標籤「,」會議標籤「和」季節「。
在Python中,我通過數據迭代如下:
from lxml import etree
doc = etree.parse('thisxmlfile.xml')
for s in doc.xpath('//season'):
for c in doc.xpath('//conference'):
for t in doc.xpath('//conference/division/team'):
print s.get('season'), c.get('label'), t.get('city'), t.get('name'), t.get('alias')
但當然,它通過所有「團隊」標籤兩次迭代 - 每進行一次「會議」標籤。我想要做的就是遍歷所有「團隊」標籤,並獲得父母「分標籤」,父母「會議標籤」和父母「季節季節」。
肯定我需要引用XPath軸,並尋找一些幫助?
我在尋找的輸出是:
2012 AFC Buffalo Bills Buf
2012 AFC Miami Dolphins Mia
2012 AFC New England Patriots NE
.
.
.
2012 NFC New Orleans Saints NO
2012 NFC Tampa Bay Buccaneers TB
2012 NFC Carolina Panthers Car
注:上面的輸出不包括「師標籤」,但一旦我弄清楚如何獲得「會議標籤」,它應該是簡單。
在此先感謝您的幫助。
非常完美 - 非常感謝你! – user2063225 2013-02-13 15:14:07