[Tutor] xml parsing without a root element
rail.shafigulin at gmail.com
Tue Aug 30 20:58:37 CEST 2011
On Tue, Aug 30, 2011 at 2:20 PM, Peter Otten <__peter__ at web.de> wrote:
> rail shafigulin wrote:
> > hello everyone.
> > i need to parse a an xml-like file. the problem that i'm facing is that
> > this file doesn't have the root element but in all other terms it is the
> > same as xml, i.e
> > <tag1>
> > </tag1>
> > <tag2>
> > </tag2>
> > <tag3/>
> > does anybody know if there is a module in python that allows to process
> > xml file without a root element? i tried ElementTree but it didn't work.
> There may be more sophisticated ways, but I'd start with a simple idea: add
> a root element to your data and have ElementTree parse the result.
> $ cat almost.xml
> $ cat xml_no_root.py
> from StringIO import StringIO
> from xml.etree.ElementTree import ElementTree
> filename = "almost.xml"
> tree = ElementTree()
> with open(filename, "rb") as f:
> data = f.read()
> pseudo_file = StringIO("<root>%s</root>" % data )
> for link in tree.getiterator("a"):
> print link.text
> $ python xml_no_root.py
> If the file is large you can read the file in smaller chunks. Have a look
> the ElementTree.parse() source code to see how to do that.
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
thanks everyone. i'm going to encapsulate the file with the root tag
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor