0

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")
2
  • Insert all the code in the code tag. Commented Dec 24, 2010 at 10:34
  • In short, to answer your question, yes it is possible. A suggestion though: Try and reformat your question as it is difficult to read. Commented Dec 24, 2010 at 10:36

6 Answers 6

9
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()

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Alot mate this saved me a hell lot of time
@Rakesh: Glad to help - so - flag it as a solution!
4

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]

Comments

1

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

1 Comment

@kame Then install it.
0

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

Comments

0

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

Comments

0

Check out BeautifulSoup

2 Comments

"The BeautifulSoup class is full of web-browser-like heuristics for divining the intent of HTML authors. But XML doesn't have a fixed tag set, so those heuristics don't apply. So BeautifulSoup doesn't do XML very well." -- crummy.com/software/BeautifulSoup/…
@Tomasz Elendt: The BeautifulSoup module has a separate class - BeautifulStoneSoup - which should be used to parse XML.

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.