Reputation: 127
I Got following XML from Amazon Web-Service.
<aws:UrlInfoResponse>
<aws:Response>
<aws:OperationRequest>
<aws:RequestId>8789797</aws:RequestId>
</aws:OperationRequest>
<aws:UrlInfoResult>
<aws:Alexa>
<aws:TrafficData>
<aws:DataUrl type="canonical">google.com/</aws:DataUrl>
<aws:Rank>1</aws:Rank>
</aws:TrafficData>
</aws:Alexa>
</aws:UrlInfoResult>
<aws:ResponseStatus>
<aws:StatusCode>Success</aws:StatusCode>
</aws:ResponseStatus>
</aws:Response>
</aws:UrlInfoResponse>
When i try to extract the rank.
xmldoc = minidom.parse(response)
itemlist = xmldoc.getElementsByTagName('aws:Rank')[0]
xmlData=itemlist.replace('<aws:Rank>','').replace('</aws:Rank>','')
print xmlData
It give me error.
AttributeError: Element instance has no attribute 'replace'
Upvotes: 1
Views: 1032
Reputation: 26
The issue here is that you are trying to use replace on an XML element, which is not a list and isn't a string, which would have the .replace().
Since you are picking out the element (which is an Element object) by using =getElementsByTagName('aws:Rank')[0]
, you only have one thing to work on.
the data that you want can be reached with:
itemlist.firstChild.data
or
itemlist.firstChild.nodeValue
(@root, you had this right, I don't know why you got downvoted)
Now I had some trouble parsing that XML because the namespace wasnt bound, but that wasn't a biggie.
what would likely be clearer is the snippet as such:
xmldoc = minidom.parse(response)
xmlElement = xmldoc.getElementsByTagName('aws:Rank')[0]
xmlData = xmlElement.firstChild.nodeValue
print xmlData
But in all honesty, you will probably want to check out the info on the Element object in minidom:
http://docs.python.org/2/library/xml.dom.html#dom-element-objects
Upvotes: 1