简体   繁体   English

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

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

I have the following XML file called Artists.xml, which contains information on several artists as shown bellow: 我有以下名为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>

I am using the Python library ElementTree in order to extract the content of all tags. 我正在使用Python库ElementTree来提取所有标签的内容。 So far this is the Python code I have written: 到目前为止,这是我编写的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)

Despite that, when I run the script I see no inputs in my console. 尽管如此,当我运行脚本时,我在控制台中看不到任何输入。 I would like to see something like : 7 USA Piece of Me 2011, 8 USA Rock Your Body 2004. Could someone help me understand what I am doing wrong? 我希望看到类似的东西: 7 USA Piece of Me 2011, 8 USA Rock Your Body 2004.有人能帮我理解我做错了吗? Thanks in advance! 提前致谢!

using xml.etree.ElementTree 使用xml.etree.ElementTree

test.xml: 的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>

Hence : 因此

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())

OUTPUT : 输出

7
USA
Piece of Me
8
USA
Rock Your Body

Process finished with exit code 0

A generic approach. 一般方法。 Convert the XML to dict and print the dict. 将XML转换为dict并打印dict。 (The file 55726013.xml contains your sample data). (文件55726013.xml包含您的示例数据)。 As you can see the code has zero knowledge about the XML structure. 如您所见,代码对XML结构没有任何了解。

import xmltodict
import json

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

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

Output 产量

{
    "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.

相关问题 如何使用 Python ElementTree 从 XML 文件中的不同子级元素中提取相关属性 - how can I extract related attributes from different child level elements from XML file with Python ElementTree Python XML ElementTree删除所有元素 - Python XML ElementTree Removing All Elements python elementtree - 如何在xml中查找具有特定属性的所有元素 - python elementtree - how to find all elements in xml with certain attribute 如何使用python中的xml.etree.ElementTree解析当前节点中的所有子元素和孙元素元素 - How to parse all children and grandchildren elements from a current node using xml.etree.ElementTree in python How to get all relevant fields from a XML file into a pandas dataframe in Python using xml.etree.ElementTree? - How to get all relevant fields from a XML file into a pandas dataframe in Python using xml.etree.ElementTree? 使用elementTree获取XML的所有元素 - Get all elements of XML with elementTree 如何使用 Python elementTree 提取 xml 数据中的特定元素 - How to extract particular elements in xml data using Python elementTree 如何提取与 Python 内容相关的所有 PDF 标签? - How can I extract all PDF Tags related to content with Python? 如何使用ElementTree for Python遍历所有XML元素并将逻辑应用于每个Element的值 - How to iterate through all XML Elements and apply logic to each Element's value with ElementTree for Python 如何使用 Python ElementTree -Glue Job 提取文件 xml 属性 - How to extract file xml attribute using Python ElementTree -Glue Job
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM