0

Using the XML below as an example, I'm trying to get the content of all <d:LayerXml> tags and add them to an array. To parse the XML I'm using ElementTree.

I first treid to access the XML elements using there names, but this failed as apparently there are no elements named 'entry' -

root = ET.fromstring(r.text)
for child in root:
    if child.tag == entry':
        print child.attirb

After I printed out all of the child tags (print child.tag), I noticed that each was suffixed with the xmlns supplied in the roor element. For example, 'entry' was actually '{http://www.w3.org/2005/Atom}'.

So next I tried to access elements using that suffix, but it just failed with a syntax error.

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

So given the following XML sample, how could I add the content of all <d:LayerXml> elements to an array. To clarify, in this case the array would contain I want this and 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/&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>

1 Answer 1

2

You haven't said which syntax error you got, the following gives the wanted result for me:

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)

Actually, it seems you can also use

values = [value.text for value in feed.findall('.//{http://schemas.microsoft.com/ado/2007/08/dataservices}LayerXml')]

or

values = [value.text for value in feed.findall('.//d:LayerXml', { 'd' : 'http://schemas.microsoft.com/ado/2007/08/dataservices' })]

if you don't want to list the full path.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that appears to be doing the job. And FYI, the sytax error I saw literally said "SyntaxError: invalid syntax", and pointed to the curly brackets. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.