How to parse XML of nested tags in Python
<p>I have following XML.</p>

<pre><code>&lt;component name="QUESTIONS"&gt;
    &lt;topic name="Chair"&gt; 
        &lt;state&gt;active&lt;/state&gt; 
        &lt;subtopic name="Wooden"&gt;
            &lt;links&gt; 
                &lt;link videoDuration="" youtubeId="" type="article"&gt;
                    &lt;label&gt;Understanding Wooden Chair&lt;/label&gt;
                    &lt;url&gt;http://abcd.xyz.com/1111?view=app&lt;/url&gt;
                &lt;/link&gt; 
                &lt;link videoDuration="" youtubeId="" type="article"&gt;
                    &lt;label&gt;How To Assemble Wooden CHair&lt;/label&gt;
                    &lt;url&gt;http://abcd.xyz.com/2222?view=app&lt;/url&gt;
                &lt;/link&gt; 
                &lt;link videoDuration="11:35" youtubeId="Qasefrt09_2" type="video"&gt;
                    &lt;label&gt;Wooden Chair Tutorial&lt;/label&gt;
                    &lt;url&gt;/&lt;/url&gt;
                &lt;/link&gt; 
                &lt;link videoDuration="1:06" youtubeId="MSDVN235879" type="video"&gt;
                    &lt;label&gt;How To Access Wood&lt;/label&gt;
                    &lt;url&gt;/&lt;/url&gt;
                &lt;/link&gt; 
            &lt;/links&gt;
        &lt;/subtopic&gt;
    &lt;/topic&gt; 
    &lt;topic name="Table"&gt; 
        &lt;state&gt;active&lt;/state&gt; 
        &lt;subtopic name=""&gt;
            &lt;links&gt; 
                &lt;link videoDuration="" youtubeId="" type="article"&gt;
                    &lt;label&gt;Understanding Tables&lt;/label&gt;
                    &lt;url&gt;http://abcd.xyz.com/3333?view=app&lt;/url&gt;
                &lt;/link&gt; 
                &lt;link videoDuration="" youtubeId="" type="article"&gt;
                    &lt;label&gt;Set-up Table&lt;/label&gt;
                    &lt;url&gt;http://abcd.xyz.com/4444?view=app&lt;/url&gt;
                &lt;/link&gt; 
                &lt;link videoDuration="" youtubeId="" type="article"&gt;
                    &lt;label&gt;How To Change table&lt;/label&gt;
                    &lt;url&gt;http://abcd.xyz.com/5555?view=app&lt;/url&gt;
                &lt;/link&gt; 
            &lt;/links&gt;
        &lt;/subtopic&gt; 
    &lt;/topic&gt; 
&lt;/component&gt;
</code></pre>

<p>I am trying to parse this xml in python and creating an <code>URL array</code> which will contain:
1. All the http urls present in the xml
2. For the link tab if youtube is present then capture that and prepare youtube url and add it to <code>URL array</code>.</p>

<p>I have following code, but it is not giving me url and links.</p>

<pre><code>from xml.etree import ElementTree

with open('faq.xml', 'rt') as f:
    tree = ElementTree.parse(f)

for node in tree.iter():
    print node.tag, node.attrib.get('url')

for node in tree.iter('outline'):
    name = node.attrib.get('link')
    url = node.attrib.get('url')
    if name and url:
        print '  %s :: %s' % (name, url)
    else:
        print name
</code></pre>

<p>How can I achieve this to get all urls?</p>

<p>developed the following code based on below answers:
Problem with following is, it is printing just 1 url not all.</p>

<pre><code>from xml.etree import ElementTree

def fetch_faq_urls():
    url_list = []
    with open('faq.xml', 'rt') as f:
        tree = ElementTree.parse(f)

    for link in tree.iter('link'):
        youtube = link.get('youtubeId')
        if youtube:
            print "https://www.youtube.com/watch?v=" + youtube
            video_url = "https://www.youtube.com/watch?v=" + youtube
            url_list.append(video_url)
            # print "youtubeId", link.find('label').text, '???'
        else:
            print link.find('url').text
            article_url = link.find('url').text
            url_list.append(article_url)
            # print 'url', link.find('label').text, 
      return url_list

faqs = fetch_faq_urls()
print faqs
</code></pre>

<p>Take a look at <a href="https://github.com/martinblech/xmltodict" rel="nofollow">xmltodict</a>.</p>

<pre><code>&gt;&gt;&gt; print(json.dumps(xmltodict.parse("""
...  &lt;mydocument has="an attribute"&gt;
...    &lt;and&gt;
...      &lt;many&gt;elements&lt;/many&gt;
...      &lt;many&gt;more elements&lt;/many&gt;
...    &lt;/and&gt;
...    &lt;plus a="complex"&gt;
...      element as well
...    &lt;/plus&gt;
...  &lt;/mydocument&gt;
...  """), indent=4))
{
    "mydocument": {
        "@has": "an attribute", 
        "and": {
            "many": [
                "elements", 
                "more elements"
            ]
        }, 
        "plus": {
            "@a": "complex", 
            "#text": "element as well"
        }
    }
}
</code></pre>

python xml