Python Minidom Parsing File Objects
Question: I wrote a code using minidom which takes an `xml` script, opens it as a file
object and then parses that file object. Not only that, but I want the script
to open multiple files that are all contained in a folder, and parse each one
individually.

An example of the `xml` script is:

    
    
    <?xml version="1.0"?>
    <Data>
      <data1>1</data1>
      <data2>2</data2>
      <data3>3</data3>
      <Sub_data>
        <sub_data1>0.1111111111111</sub_data1>
        <sub_data2>0.2222222222222</sub_data2>
        ... and so on.
    

i.e., it's pretty standard.

Now, my code looks like this:

    
    
    import os
    import io
    from xml.dom import minidom
    
    #folder where xml files are located
    indir = '/foo/bar/docs/'
    
        masterlist = []
    
        for root, dirs, filenames in os.walk(indir):
          for f in filenames:
            row = []
            fsock = io.open(indir + f, mode = 'rt', encoding = 'cp1252')
            xmldoc = minidom.parse(fsock)
            ...
    

and the error I am getting is:

    
    
    Traceback (most recent call last): File "kgp_2.py", line 34, in
    <module> xmldoc = minidom.parse(fsock) File
    "/usr/lib/python2.7/xml/dom/minidom.py", line 1918, in parse return
    expatbuilder.parse(file) File
    "/usr/lib/python2.7/xml/dom/expatbuilder.py", line 928, in parse
    result = builder.parseFile(file) File
    "/usr/lib/python2.7/xml/dom/expatbuilder.py", line 211, in parseFile
    parser.Parse("", True) xml.parsers.expat.ExpatError: no element found:
    line 203, column 1381
    

Now, when I make the change:

    
    
    fsock = io.open(indir + filenames[0], mode = 'rt', encoding = 'cp1252')
    

this works fine, that is, it opens the first file in the folder; but I want to
parse all the files in the folder. When I do a loop like:

    
    
    m = 0
    ... in loop:
    fsock = io.open(indir + filenames[m], mode = 'rt', encoding = 'cp1252')
    ...
    m = m+1
    

I get the original error.

The reason I am using the io library instead of the usual file open function
is that a previous stack overflow article recommended it. Using:

    
    
    fsock = open(indir + filenames[0])
    

like before, gets no error, but:

    
    
    fsock = open(indir + f)
    

or

    
    
    #with a loop over m, like above
    fsock = open(infir + filenames[m])
    

get the same error as above.

A strange problem. When I print the filenames they are correct. And they
**are** being opened, there's no error there. It's the parser that just won't
parse the object files, even with `filenames[m]` where `m = 0`, surely this
should be no problem?

EDIT: [Parsing document with python
minidom](http://stackoverflow.com/questions/3625897/parsing-document-with-
python-minidom)

in this post they had a similar problem, the resolution was to use

    
    
        xmldoc.seek(0)
    

however, for me this returns

    
    
    Traceback (most recent call last):
    File "kgp_2.py", line 45, in <module>
    xmldoc.seek(0)
    AttributeError: Document instance has no attribute 'seek'
    

**EDIT 2: THIS HAS BEEN RESOLVED. IT WAS A CASE OF A CORRUPTED INPUT XML
FILE.**


Answer: Are you sure the XML data contained in all XML files is correct? Perhaps one
is empty an you have to handle such Exception. Anyhow I recommend you to use
`xml.etree`
[doc](https://docs.python.org/2/library/xml.etree.elementtree.html).

