Generating Nested Lists From Xml Doc
Working in python, my goal is to parse through an XML doc I made and create a nested list of lists in order to access them later and parse the feeds. The XML doc resembles the foll
Solution 1:
From your description, a dictionary with keys according to the source name and values according to the feed lists might do the trick.
Here is one way to construct such a beast:
from lxml import etree
from pprint import pprint
news_sources = {
source.attrib['source'] : [feed.textfor feed in source.xpath('./f')]
for source in etree.parse('x.xml').xpath('/sources/sourceList')}
pprint(news_sources)
Another sample, without lxml
or xpath
:
import xml.etree.ElementTreeasETfrom pprint import pprint
news_sources = {
source.attrib['source'] : [feed.textfor feed in source]
for source inET.parse('x.xml').getroot()}
pprint(news_sources)
Finally, if you are allergic to list comprehensions:
import xml.etree.ElementTreeasETfrom pprint import pprint
xml = ET.parse('x.xml')
root = xml.getroot()
news_sources = {}
for sourceList inroot:
sourceListName = sourceList.attrib['source']
news_sources[sourceListName] = []
for feed insourceList:
feedName = feed.text
news_sources[sourceListName].append(feedName)
pprint(news_sources)
Post a Comment for "Generating Nested Lists From Xml Doc"