0
아래 XML을 예로 들어 모든 <d:LayerXml>
태그의 내용을 가져와 배열에 추가하려고합니다. XML을 구문 분석하려면 ElementTree를 사용하고 있습니다. 나는 자식 태그 (print child.tag
)을 모두 인쇄 후특정 XML emements의 값을 가져 와서 Python의 배열에 추가하십시오.
root = ET.fromstring(r.text)
for child in root:
if child.tag == entry':
print child.attirb
- 같은 분명히 '항목'라는 이름의 요소가없는
은 내가 먼저 TREID이 이름을 사용하여 XML 요소에 액세스 할 수 있지만 실패 나는 각각이 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
그래서 내가 배열에 대한 모든
<d:LayerXml>
요소의 콘텐츠를 추가 할 수있는 방법을 다음과 같은 XML 샘플을, 주어진. 명확하게하기 위해이 경우 배열에는
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>
감사합니다. 그 일을하는 것으로 보입니다. 그리고 참고로 필자가 보았던 sytax 오류는 "SyntaxError : invalid syntax"라고 말하면서 중괄호를 가리켰다. 다시 한번 감사드립니다. –