【发布时间】:2017-07-22 05:30:26
【问题描述】:
以下面的 XML 为例,我试图获取所有 <d:LayerXml> 标记的内容并将它们添加到数组中。为了解析 XML,我使用 ElementTree。
我第一次尝试使用那里的名称访问 XML 元素,但是这失败了,因为显然没有名为“条目”的元素 -
root = ET.fromstring(r.text)
for child in root:
if child.tag == entry':
print child.attirb
在我打印出所有子标签 (print child.tag) 后,我注意到每个标签都以 roor 元素中提供的 xmlns 为后缀。例如,“entry”实际上是“{http://www.w3.org/2005/Atom}”。
所以接下来我尝试使用该后缀访问元素,但它只是因为语法错误而失败。
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 示例,我如何将所有 <d:LayerXml> 元素的内容添加到数组中。澄清一下,在这种情况下,数组将包含 I want this 和 I want this, too。
<?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/"datetime'2017-03-02T11%3A46%3A37.1271167Z'"">
<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/"datetime'2017-03-02T11%3A46%3A37.1271167Z'"">
<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>
【问题讨论】:
标签: python xml elementtree