Reading XML on python 2.1

300 Views Asked by At

So, i'm trying to process a XML file on python.

I'm using minidom as I'm in python 2.1 and there's no change to updating to 3.6. Currently, I have this

import xml.dom.minidom as minidom
import socket
print 'Getting the xml file'
# Get the xml contents
file = open('<filepath>')
#print file
# Get the root of the configuration file
print 'Parsing the xml'
procs = minidom.parse(file)

But I'm getting this error

Error getting

Any idea? Or, better yet, another way to parse xml without me having to write my own parser...

1

There are 1 best solutions below

0
CJLopez On BEST ANSWER

So, I was able to get this working

For starters, after trying to convince to either update or install a plugin, I was notified that all python scripts are ran on jython, which mean, I have several java libraries to my disposal (wish they could had told me this quite sooner)

So, after some investigation on xml processing on jython, I found out that using Xerces and xas was the key

This is the code I finally used if any one would like to know

from java.io import StringReader

import org.xml.sax as sax
import org.apache.xerces.parsers.DOMParser as domparser

parser = domparser()
document = open('<path to file>').read()
parser.reset()
documentIS = sax.InputSource(StringReader(document))
parser.parse(documentIS)
domtree = parser.getDocument()
results = domtree.getElementsByTagName('<tag name>')
for ix in range(results.getLength()):
    item = results.item(ix).getAttribute("<attribute name>")

Hope someone else finds this usefull