如何在Python中解析嵌套标签的XML

时间:2016-09-29 02:43:21

标签: python xml

我有以下XML。

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

我试图在python中解析这个xml并创建一个URL array,其中包含: 1. xml中存在的所有http url 2.对于链接选项卡,如果存在youtube,则捕获并准备youtube网址并将其添加到URL array

我有以下代码,但它没有给我网址和链接。

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

如何实现这一目标以获取所有网址?

基于以下答案开发了以下代码: 以下问题是,它只打印1个网址。

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

2 个答案:

答案 0 :(得分:1)

您想要的信息位于<link>下,因此请仔细阅读这些信息。使用get()获取youtube ID,find()获取子<url>对象。

from xml.etree import ElementTree

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

for link in tree.iter('link'):
    youtube = link.get('youtubeId')
    if youtube:
        print "youtube", link.find('label').text, '???'
    else:
        print 'url', link.find('label').text, link.find('url').text

答案 1 :(得分:0)

看看xmltodict

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