获取特定XML元素的值,并将它们添加到Python中的数组中

获取特定XML元素的值,并将它们添加到Python中的数组中,python,xml,elementtree,Python,Xml,Elementtree,以下面的XML为例,我试图获取所有标记的内容,并将它们添加到数组中。为了解析XML,我使用ElementTree 我首先使用treid来访问使用这些名称的XML元素,但这失败了,因为显然没有名为“entry”的元素- root = ET.fromstring(r.text) for child in root: if child.tag == entry': print child.attirb 在我打印出所有的子标记(print child.tag)之后,我注意到每个

以下面的XML为例,我试图获取所有
标记的内容,并将它们添加到数组中。为了解析XML,我使用ElementTree

我首先使用treid来访问使用这些名称的XML元素,但这失败了,因为显然没有名为“entry”的元素-

root = ET.fromstring(r.text)
for child in root:
    if child.tag == entry':
        print child.attirb
在我打印出所有的子标记(
print child.tag
)之后,我注意到每个标记都以roor元素中提供的xmlns作为后缀。例如,“entry”实际上是“{}”

所以接下来我尝试使用这个后缀访问元素,但由于语法错误而失败了

root = ET.fromstring(r.text)
for child in root:
    if child.tag == '{http://www.w3.org/2005/Atom}entry':
        layerXML = child.{http://www.w3.org/2005/Atom}content
# Also tried - layerXML = child.'{http://www.w3.org/2005/Atom}content'
        print layerXML
因此,给定以下XML示例,如何将所有
元素的内容添加到数组中。为了澄清,在这种情况下,数组将包含
我想要这个
我也想要这个

<?xml version="1.0" encoding="utf-8"?>
<feed xml:base="https://tablestore.somewhere.com/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml">
    <id>https://tablestore.somewhere.com/TableName</id>
    <title type="text">TableName</title>
    <updated>2017-03-02T12:01:04Z</updated>
    <link rel="self" title="TableName" href="TableName" />
    <entry m:etag="W/&quot;datetime'2017-03-02T11%3A46%3A37.1271167Z'&quot;">
        <id>https://tablestore.somewhere.com/TableName(PartitionKey='PartitonKey',RowKey='layer1-tileMatrixSet')</id>
        <category term="tablestore.TableName" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <link rel="edit" title="TableName" href="TableName(PartitionKey='PartitonKey',RowKey='layer1-tileMatrixSet')" />
        <title />
        <updated>2017-03-02T12:01:04Z</updated>
        <author>
            <name />
        </author>
        <content type="application/xml">
            <m:properties>
                <d:PartitionKey>PartitonKey</d:PartitionKey>
                <d:RowKey>RowKey</d:RowKey>
                <d:Timestamp m:type="Edm.DateTime">2017-03-02T11:46:37.1271167Z</d:Timestamp>
                <d:AuthType>basic</d:AuthType>
                <d:Credentials>CREDENTIALS1</d:Credentials>
                <d:Layer>layer1</d:Layer>
                <d:LayerXml>I want this</d:LayerXml>
                <d:Service>https://www.google.co.uk</d:Service>
                <d:TileMatrixSet>tileMatrixSet</d:TileMatrixSet>
            </m:properties>
        </content>
    </entry>
    <entry m:etag="W/&quot;datetime'2017-03-02T11%3A46%3A37.1271167Z'&quot;">
        <id>https://tablestore.somewhere.com/TableName(PartitionKey='PartitonKey',RowKey='layer2-tileMatrixSet')</id>
        <category term="tablestore.TableName" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <link rel="edit" title="TableName" href="TableName(PartitionKey='PartitonKey',RowKey='layer2-tileMatrixSet')" />
        <title />
        <updated>2017-03-02T12:01:04Z</updated>
        <author>
            <name />
        </author>
        <content type="application/xml">
            <m:properties>
                <d:PartitionKey>PartitonKey</d:PartitionKey>
                <d:RowKey>RowKey</d:RowKey>
                <d:Timestamp m:type="Edm.DateTime">2017-03-02T11:46:37.1271167Z</d:Timestamp>
                <d:AuthType>basic</d:AuthType>
                <d:Credentials>CREDENTIALS1</d:Credentials>
                <d:Layer>layer2</d:Layer>
                <d:LayerXml>I want this, too</d:LayerXml>
                <d:Service>https://www.google.co.uk</d:Service>
                <d:TileMatrixSet>tileMatrixSet</d:TileMatrixSet>
            </m:properties>
        </content>
    </entry>
</feed>

https://tablestore.somewhere.com/TableName
表名
2017-03-02T12:01:04Z
https://tablestore.somewhere.com/TableName(PartitionKey='PartitonKey',RowKey='layer1-tileMatrix集')
2017-03-02T12:01:04Z
PartitonKey
罗基
2017-03-02T11:46:37.1271167Z
基本的
证书1
第1层
我想要这个
https://www.google.co.uk
TileMatrix集
https://tablestore.somewhere.com/TableName(PartitionKey='PartitonKey',RowKey='layer2-tileMatrix集')
2017-03-02T12:01:04Z
PartitonKey
罗基
2017-03-02T11:46:37.1271167Z
基本的
证书1
第二层
我也想要这个
https://www.google.co.uk
TileMatrix集

您还没有说您遇到了哪种语法错误,下面是我想要的结果:

from xml.etree import ElementTree as ET
xml = '''<?xml version="1.0" encoding="utf-8"?>
<feed xml:base="https://tablestore.somewhere.com/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml">
    <id>https://tablestore.somewhere.com/TableName</id>
    <title type="text">TableName</title>
    <updated>2017-03-02T12:01:04Z</updated>
    <link rel="self" title="TableName" href="TableName" />
    <entry m:etag="W/&quot;datetime'2017-03-02T11%3A46%3A37.1271167Z'&quot;">
        <id>https://tablestore.somewhere.com/TableName(PartitionKey='PartitonKey',RowKey='layer1-tileMatrixSet')</id>
        <category term="tablestore.TableName" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <link rel="edit" title="TableName" href="TableName(PartitionKey='PartitonKey',RowKey='layer1-tileMatrixSet')" />
        <title />
        <updated>2017-03-02T12:01:04Z</updated>
        <author>
            <name />
        </author>
        <content type="application/xml">
            <m:properties>
                <d:PartitionKey>PartitonKey</d:PartitionKey>
                <d:RowKey>RowKey</d:RowKey>
                <d:Timestamp m:type="Edm.DateTime">2017-03-02T11:46:37.1271167Z</d:Timestamp>
                <d:AuthType>basic</d:AuthType>
                <d:Credentials>CREDENTIALS1</d:Credentials>
                <d:Layer>layer1</d:Layer>
                <d:LayerXml>I want this</d:LayerXml>
                <d:Service>https://www.google.co.uk</d:Service>
                <d:TileMatrixSet>tileMatrixSet</d:TileMatrixSet>
            </m:properties>
        </content>
    </entry>
    <entry m:etag="W/&quot;datetime'2017-03-02T11%3A46%3A37.1271167Z'&quot;">
        <id>https://tablestore.somewhere.com/TableName(PartitionKey='PartitonKey',RowKey='layer2-tileMatrixSet')</id>
        <category term="tablestore.TableName" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <link rel="edit" title="TableName" href="TableName(PartitionKey='PartitonKey',RowKey='layer2-tileMatrixSet')" />
        <title />
        <updated>2017-03-02T12:01:04Z</updated>
        <author>
            <name />
        </author>
        <content type="application/xml">
            <m:properties>
                <d:PartitionKey>PartitonKey</d:PartitionKey>
                <d:RowKey>RowKey</d:RowKey>
                <d:Timestamp m:type="Edm.DateTime">2017-03-02T11:46:37.1271167Z</d:Timestamp>
                <d:AuthType>basic</d:AuthType>
                <d:Credentials>CREDENTIALS1</d:Credentials>
                <d:Layer>layer2</d:Layer>
                <d:LayerXml>I want this, too</d:LayerXml>
                <d:Service>https://www.google.co.uk</d:Service>
                <d:TileMatrixSet>tileMatrixSet</d:TileMatrixSet>
            </m:properties>
        </content>
    </entry>
</feed>
'''
feed = ET.fromstring(xml)
values = [value.text for value in feed.findall('{http://www.w3.org/2005/Atom}entry/{http://www.w3.org/2005/Atom}content/{http://schemas.microsoft.com/ado/2007/08/dataservices/metadata}properties/{http://schemas.microsoft.com/ado/2007/08/dataservices}LayerXml')]
print(values)


如果您不想列出完整路径。

您还没有说您遇到了哪种语法错误,下面为我提供了想要的结果:

from xml.etree import ElementTree as ET
xml = '''<?xml version="1.0" encoding="utf-8"?>
<feed xml:base="https://tablestore.somewhere.com/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml">
    <id>https://tablestore.somewhere.com/TableName</id>
    <title type="text">TableName</title>
    <updated>2017-03-02T12:01:04Z</updated>
    <link rel="self" title="TableName" href="TableName" />
    <entry m:etag="W/&quot;datetime'2017-03-02T11%3A46%3A37.1271167Z'&quot;">
        <id>https://tablestore.somewhere.com/TableName(PartitionKey='PartitonKey',RowKey='layer1-tileMatrixSet')</id>
        <category term="tablestore.TableName" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <link rel="edit" title="TableName" href="TableName(PartitionKey='PartitonKey',RowKey='layer1-tileMatrixSet')" />
        <title />
        <updated>2017-03-02T12:01:04Z</updated>
        <author>
            <name />
        </author>
        <content type="application/xml">
            <m:properties>
                <d:PartitionKey>PartitonKey</d:PartitionKey>
                <d:RowKey>RowKey</d:RowKey>
                <d:Timestamp m:type="Edm.DateTime">2017-03-02T11:46:37.1271167Z</d:Timestamp>
                <d:AuthType>basic</d:AuthType>
                <d:Credentials>CREDENTIALS1</d:Credentials>
                <d:Layer>layer1</d:Layer>
                <d:LayerXml>I want this</d:LayerXml>
                <d:Service>https://www.google.co.uk</d:Service>
                <d:TileMatrixSet>tileMatrixSet</d:TileMatrixSet>
            </m:properties>
        </content>
    </entry>
    <entry m:etag="W/&quot;datetime'2017-03-02T11%3A46%3A37.1271167Z'&quot;">
        <id>https://tablestore.somewhere.com/TableName(PartitionKey='PartitonKey',RowKey='layer2-tileMatrixSet')</id>
        <category term="tablestore.TableName" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <link rel="edit" title="TableName" href="TableName(PartitionKey='PartitonKey',RowKey='layer2-tileMatrixSet')" />
        <title />
        <updated>2017-03-02T12:01:04Z</updated>
        <author>
            <name />
        </author>
        <content type="application/xml">
            <m:properties>
                <d:PartitionKey>PartitonKey</d:PartitionKey>
                <d:RowKey>RowKey</d:RowKey>
                <d:Timestamp m:type="Edm.DateTime">2017-03-02T11:46:37.1271167Z</d:Timestamp>
                <d:AuthType>basic</d:AuthType>
                <d:Credentials>CREDENTIALS1</d:Credentials>
                <d:Layer>layer2</d:Layer>
                <d:LayerXml>I want this, too</d:LayerXml>
                <d:Service>https://www.google.co.uk</d:Service>
                <d:TileMatrixSet>tileMatrixSet</d:TileMatrixSet>
            </m:properties>
        </content>
    </entry>
</feed>
'''
feed = ET.fromstring(xml)
values = [value.text for value in feed.findall('{http://www.w3.org/2005/Atom}entry/{http://www.w3.org/2005/Atom}content/{http://schemas.microsoft.com/ado/2007/08/dataservices/metadata}properties/{http://schemas.microsoft.com/ado/2007/08/dataservices}LayerXml')]
print(values)


如果您不想列出完整路径。

谢谢,这似乎可以完成任务。仅供参考,我看到的sytax错误字面上是“SyntaxError:invalid syntax”,并指向花括号。再次感谢。谢谢你,这看起来很有效。仅供参考,我看到的sytax错误字面上是“SyntaxError:invalid syntax”,并指向花括号。再次感谢。
values = [value.text for value in feed.findall('.//d:LayerXml', { 'd' : 'http://schemas.microsoft.com/ado/2007/08/dataservices' })]