PoweredByCoffee
PoweredByCoffee

Reputation: 1193

Python Reading XML Child

Trying to parse this XML but I can't seem to figure out quite where I'm going wrong.

Snippet of XML:

<thexml timestamp="2017-01-02T10:17:41">
<event="41" date="2017-04-01" id="5543" time="09:30:00" type="seat" link="na"></event>
</thexml>

I'm trying:

DOMTree = parseString(response.content)
collection = DOMTree.documentElement
selections = collection.getElementsByTagName("event")
for select in selections:
 print "event found"

This seems to work it triggers on the events in the XML. Trying to get the type, for example, is stumping me with this format.

tags = select.getElementsByTagName("type")

When I use this the string for tags becomes which suggests it found it. But I'm not sure how to actually read the string of the child. I've been trying variations on:

print type.childNodes[0].data
print type.childNodes.data
print type.data

Am I missing something really obvious here? I parse a bunch of XML's but this format is throwing me a bit. Would appreciate a point in the right direction.

Upvotes: 1

Views: 53

Answers (1)

Dekel
Dekel

Reputation: 62666

You still had a problem in your xml.

Here is the fix (and how to extract the relevant attributes):

In [17]: c = """<thexml timestamp="2017-01-02T10:17:41">
    ...: <event date="2017-04-01" id="5543" time="09:30:00" type="seat" link="na"></event>
    ...: </thexml>
    ...: """
In [18]: DOMTree = parseString(c)
In [19]: collection = DOMTree.documentElement
In [20]: s = collection.getElementsByTagName('event')
In [21]: for e in s:
    ...:     print(e.getAttribute('type'))
    ...:
seat

Note that in your example - type is an Attribute (And not Node) so you can't use getElementsByTagName("type"), you will need to use getAttribute

Upvotes: 1

Related Questions