John Kilo
John Kilo

Reputation: 123

Python, extract date from XML

Apologies, my Python knowledge is pretty non-existant. I need to extract a date from some XML which is in a format similar to:

<Header>
<Version>1.0</Version>
....
    <cd:Data>...</Data>
    .....
    <cd:DateReceived>20070620171524</cd:DateReceived>

From looking around here I found something similar

#!/usr/bin/python
from xml.dom.minidom import parse  
import xml.dom.minidom

# Open XML document using minidom parser
DOMTree = xml.dom.minidom.parse("date.xml")
collection = DOMTree.documentElement

print collection.getElementsByTagName("cd:DateReceived").item(0)

However this only prints the Hex value:

<DOM Element: cd:DateReceived at 0x1529e0>

How can I get the date 20070620171524?

I've tried using the following

#!/usr/bin/python
from xml.dom.minidom import parse  
import xml.dom.minidom

# Open XML document using minidom parser
DOMTree = xml.dom.minidom.parse("date.xml")
collection = DOMTree.documentElement

date = cd:DateReceived[0].firstChild.nodeValue
print date

but it gives an error as it doesn't like the "cd" part of the tag

date = cd:DateReceived[0].firstChild.nodeValue
         ^
SyntaxError: invalid syntax

Any help would be appreciated. Thanks!

Upvotes: 0

Views: 733

Answers (1)

kibitzer
kibitzer

Reputation: 4589

collection.getElementsByTagName("cd:DateReceived").item(0) returns a node. from that node, you can get nodeValue

Upvotes: 1

Related Questions