1
from xml.dom.minidom import parseString, parse

dom = parse('response.xml')

xmlTag = dom.getElementsByTagName('link')[0].toxml()

How do I get the attribute 'href' of the xml for rel="alternate":

    <link rel='alternate' type='text/html' href='http://www.youtube.com/watch?v=gfyKPbic&amp;feature=youtube_gdata'/>
  <link rel='http://gdata.youtube.com/schemas/2007#video.responses' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/videos/gf7zhyKPbic/responses'/>
1
  • 1
    Among other errors, this line dom = parseString(data) dom = parse('response.xml') is invalid python syntax. Commented Nov 23, 2011 at 11:04

1 Answer 1

3
DOC = """<root>
  <link rel='alternate' type='text/html' href='http://www.youtube.com/watch?v=gfyKPbic&amp;feature=youtube_gdata'/>
  <link rel='http://gdata.youtube.com/schemas/2007#video.responses' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/videos/gf7zhyKPbic/responses'/>
</root>"""
from xml.dom.minidom import parseString, parse
dom = parseString(DOC)
hreflist= [elt.getAttribute("href") for elt in dom.getElementsByTagName('link')  if elt.getAttribute("rel")=="alternate"]
for href in hreflist:
    print(href)
Sign up to request clarification or add additional context in comments.

Comments

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.