簡體   English   中英

如何使用Python和ElementTree從XML文件的所有元素中提取所有內容?

[英]How can I extract all content from all elements of an XML file with Python and ElementTree?

我有以下名為Artists.xml的XML文件,其中包含幾個藝術家的信息,如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Artists>
<Singer name="Britney">
    <Albums>7</Albums>
    <Country>USA</County>
    <Last Single>  Piece of Me
      <Year>2011</Year>
   </Last Single>
</Singer>
<Singer name="Justin">
    <Albums>8</Albums>
    <Country>USA</County>
    <Last Single> Rock Your Body
      <Year>2004</Year>
   </Last Single>
</Singer>
</Artsts>

我正在使用Python庫ElementTree來提取所有標簽的內容。 到目前為止,這是我編寫的Python代碼:

from xml.etree import cElementTree as ET
tree = ET.parse('Artists.xml')
root = tree.getroot()
for child in root:
    for content in child:
       print(child[content].text)

盡管如此,當我運行腳本時,我在控制台中看不到任何輸入。 我希望看到類似的東西: 7 USA Piece of Me 2011, 8 USA Rock Your Body 2004.有人能幫我理解我做錯了嗎? 提前致謝!

使用xml.etree.ElementTree

的test.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Artists>
    <Singer name="Britney">
        <Albums>7</Albums>
        <Country>USA</Country>
        <LastSingle>
               Piece of Me
              <Year>2011</Year>
       </LastSingle>
    </Singer>
    <Singer name="Justin">
        <Albums>8</Albums>
        <Country>USA</Country>
        <LastSingle> Rock Your Body
          <Year>2004</Year>
       </LastSingle>
    </Singer>
</Artists>

因此

from xml.etree import ElementTree
tree = ElementTree.parse('test.xml')
root = tree.getroot()
results = root.findall('Singer')

for elem in results:
    for e in elem:
        print(e.text.strip())

輸出

7
USA
Piece of Me
8
USA
Rock Your Body

Process finished with exit code 0

一般方法。 將XML轉換為dict並打印dict。 (文件55726013.xml包含您的示例數據)。 如您所見,代碼對XML結構沒有任何了解。

import xmltodict
import json

with open('55726013.xml') as fd:
    doc = xmltodict.parse(fd.read())

print(json.dumps(doc, indent=4))

產量

{
    "Artists": {
        "Singer": [
            {
                "@name": "Britney", 
                "Albums": "7", 
                "Country": "USA", 
                "LastSingle": {
                    "Year": "2011", 
                    "#text": "Piece of Me"
                }
            }, 
            {
                "@name": "Justin", 
                "Albums": "8", 
                "Country": "USA", 
                "LastSingle": {
                    "Year": "2004", 
                    "#text": "Rock Your Body"
                }
            }
        ]
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM