![](/img/trans.png)
[英]How do I get the xml:id of an element using ElementTree in python
[英]Get XML Parent ID and all Childs underneath using ElementTree and python
我想從 XML 獲得一些信息。
這是 XML 代碼:
<dashboards>
<dashboard name="1 off">
<style/>
<size maxheight="800" maxwidth="1000" minheight="800" minwidth="1000"/>
<zones>
<zone h="100000" id="2" type-v2="layout-basic" w="100000" x="0" y="0">
<zone forceUpdate="true" h="98000" id="1" type-v2="text" w="49200" x="800" y="1000">
<formatted-text>
<run fontsize="1">
row 1
</run>
<run>
Æ
</run>
<run fontsize="2">
row 2
</run>
</formatted-text>
<zone-style>
<format attr="border-color" value="#000000"/>
<format attr="border-style" value="none"/>
<format attr="border-width" value="0"/>
<format attr="margin" value="4"/>
</zone-style>
</zone>
<zone h="98000" id="3" type-v2="text" w="49200" x="50000" y="1000">
<formatted-text>
<run>
id2
</run>
</formatted-text>
<zone-style>
<format attr="border-color" value="#000000"/>
<format attr="border-style" value="none"/>
<format attr="border-width" value="0"/>
<format attr="margin" value="4"/>
</zone-style>
</zone>
<zone-style>
<format attr="border-color" value="#000000"/>
<format attr="border-style" value="none"/>
<format attr="border-width" value="0"/>
<format attr="margin" value="8"/>
</zone-style>
</zone>
</zones>
</dashboard>
</dashboards>
我能夠在單獨運行時獲取所有 ID 和所有詳細信息,但我想獲得不同的 output。因此,對於每個元素,我想為其分配父 ID。
這是我的代碼分別獲取信息:
import xml.etree.ElementTree as et
tree = et.parse(r'C:\book3.twb')
root = tree.getroot()
dbnameRC=[]
fontRC = []
sizeRC = []
weightRC = []
f_styleRC = []
decoRC = []
colorRC= []
alignRC = []
textcontRC=[]
idRC=[]
for db in root.iter("dashboard"):
root1 = db
for z in root1.findall(".//zone[@type-v2='text']"):
idRC.append(z.get('id'))
for m in root1.findall(".//zone[@type-v2='text']/formatted-text//run"):
weightRC.append(m.get('bold'))
alignRC.append(m.get('fontalignment'))
colorRC.append(m.get('fontcolor'))
fontRC.append(m.get('fontname'))
sizeRC.append(m.get('fontsize'))
f_styleRC.append(m.get('italic'))
decoRC.append(m.get('underline'))
dbnameRC.append(db.attrib['name'])
textcontRC.append(m.text)
1.idRC 的 output 是:
['1', '3']
這是正確的,因為我們只有兩個 type-v2='text' 的 id]
['1', None, '2', None]
這也是正確的。
問題是如何編寫代碼以像這樣給出 output:
基本上我想要做的就是使用 type-v2="text" 進入區域獲取它的 id 並在下面進行所有運行並將它分配給這個特定的 id 然后移動到另一個具有不同 id 和
type-v2="text" 並在此區域下進行所有運行。
你可以代替你的第二個root1.findall()
zone.findall()
- 允許你在每次運行時對id
進行分組。
runs = []
for db in root.iter("dashboard"):
root1 = db
for zone in root1.findall(".//zone[@type-v2='text']"):
idrc = zone.get('id')
for run in zone.findall("./formatted-text//run"):
runs.append([
idrc,
run.get("bold"),
run.get("fontalignment"),
run.get("fontcolor"),
run.get("fontname"),
run.get("fontsize"),
run.get("italic"),
run.get("underline"),
db.attrib["name"],
run.text
])
Output:
>>> runs
[['1', None, None, None, None, '1', None, None, '1 off', 'row 1'],
['1', None, None, None, None, None, None, None, '1 off', 'Æ'],
['1', None, None, None, None, '2', None, None, '1 off', 'row 2'],
['3', None, None, None, None, None, None, None, '1 off', 'id2']]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.