Data from XML to Python list

2019-04-02 16:59发布

Is it possible to store data from an XML file to a list in python. For example I have an XML file with the following content:

<brochure>
<onlinePath>http://EEE</onlinePath>
<downloadPath>http://YYY</downloadPath>
<Name>ABC</Name>
<AAA>
    <P>JPG</P>
        <Q>JPG</Q>
</AAA>
</brochure>
<brochure>
<onlinePath>http://EKK</onlinePath>
<downloadPath>http://XXX</downloadPath>
<Name>DEF</Name>
<AAA>
    <P>JPG</P>
        <Q>JPG</Q>
</AAA>
</brochure>

Is it possible to store into a python list like:

onlinePath = ("http://EEE", "http://EKK")
Name = ("ABC", "DEF")

6条回答
姐就是有狂的资本
2楼-- · 2019-04-02 17:36

Yes, it is very possible. Two libraries to help you with this is ElementTree and lxml. Take a look at them.

查看更多
成全新的幸福
3楼-- · 2019-04-02 17:36

i don't think it can store it in lists bt it can store it in dictionaries since they have key:values using expat for xml

but can check the entries here

查看更多
神经病院院长
4楼-- · 2019-04-02 17:40
import lxml

xml = """
<brochures>
    <brochure>
        <onlinePath>http://EEE</onlinePath>
        <downloadPath>http://YYY</downloadPath>
        <Name>ABC</Name>
        <AAA>
            <P>JPG</P>
            <Q>JPG</Q>
        </AAA>
    </brochure>
    <brochure>
        <onlinePath>http://EKK</onlinePath>
        <downloadPath>http://XXX</downloadPath>
        <Name>DEF</Name>
        <AAA>
            <P>JPG</P>
            <Q>JPG</Q>
        </AAA>
    </brochure>
</brochures>
"""

root = lxml.etree.fromstring(xml)
mylist = root.xpath('//brochure/onlinePath/text()')

results in

['http://EEE', 'http://EKK']

Notes:

  1. I wrapped your xml in <brochures></brochures> to make it a tree instead of a forest (ie single root node);

  2. If you want to read from a file instead of a string, use lxml.etree.parse() instead of lxml.etree.fromstring()

查看更多
男人必须洒脱
5楼-- · 2019-04-02 17:41
一纸荒年 Trace。
6楼-- · 2019-04-02 17:44

For short xml file like this, I suggest you minidom.

查看更多
来,给爷笑一个
7楼-- · 2019-04-02 17:45

Hugh's solution is fine. Here is a variation that uses ElementTree (tested with Python 2.6):

from xml.etree import ElementTree

tree = ElementTree.parse("yourfile.xml")   
olp = tree.findall("//onlinePath")
mylist = [t.text for t in olp]
查看更多
登录 后发表回答