Plain old SAX and minidom useable? But how then?

D

Dobedani

Dear All,

I guess I don't know where to look for the right information. I hope
you guys can help me on the way. I want to retrieve a string from an
XML-file. If Python were to have XPath available, my problem would be
solved. The xquery string would be enough and I have already obtained
that the string. The problem is that I cannot use any add-on - like
xmllib, sax2 or elementtree - as my customers only have the so-called
stock Python install - i.e. version 2.4.1. I've seen some posts from
2½ years ago by Nelson Minar and also by Uche Ogbuji who wrote:
"Nelson says: There's the stock Python install, which barely does
anything [for XML]. That's overstated. Plain old SAX and minidom may
not be ideal, but they're useable."

Please: where then can I find examples of such use? If I cannot use
xpath, I would not mind to browse a bit - e.g. using functions like
getElementByTag() but I don't even know how to use those. TIA

Kind regards,
Dobedani
 
C

Carsten Haese

[...]
"Nelson says: There's the stock Python install, which barely does
anything [for XML]. That's overstated. Plain old SAX and minidom may
not be ideal, but they're useable."

Please: where then can I find examples of such use? If I cannot use
xpath, I would not mind to browse a bit - e.g. using functions like
getElementByTag() but I don't even know how to use those. TIA

Here's a micro-tutorial:
.... <root>
.... <thing>Parrot</thing>
.... said:
dom = xml.dom.minidom.parseString(contents)
things = dom.getElementsByTagName("thing")
print things[0].childNodes
[ said:
print things[0].childNodes[0].nodeValue Parrot
print things[0].hasAttribute("class") False
print things[0].getAttribute("class")
print things[1].childNodes
[ said:
print things[1].childNodes[0].nodeValue Holy Grail
print things[1].hasAttribute("class") True
print things[1].getAttribute("class")
special

There's also the online documentation at
http://docs.python.org/lib/module-xml.dom.minidom.html

Hope this helps,
 
S

Stefan Behnel

Hi,

calm down, minidom is not easy to use, but it can solve your problem.
I guess I don't know where to look for the right information. I hope
you guys can help me on the way. I want to retrieve a string from an
XML-file. If Python were to have XPath available, my problem would be
solved. The xquery string would be enough and I have already obtained
that the string. The problem is that I cannot use any add-on - like
xmllib, sax2 or elementtree - as my customers only have the so-called
stock Python install - i.e. version 2.4.1.

Too bad, that rules out lxml.etree (which has XPath and loads of other goodies).

Please: where then can I find examples of such use? If I cannot use
xpath, I would not mind to browse a bit - e.g. using functions like
getElementByTag() but I don't even know how to use those. TIA

Try one of these (I looked for "minidom example"):

http://www.faqs.org/docs/diveintopython/kgp_parse.html
http://docs.python.org/lib/dom-example.html
http://www.cutthecrap.biz/software/whitepapers/minidom.html

Maybe that helps?

Stefan
 
D

Dobedani

Darsten and Stefan,

Yeah, thank you very much! I actually found a good example at
http://www.python.org/doc/2.4/lib/dom-example.html

Your input was also helpful, so now I have been able to "walk" through
the XML and to retrieve the text strings I need:

doc = parse(configfile);
elems = doc.getElementsByTagName("Environment")
for elem in elems:
name = elem.getElementsByTagName("Name")[0];
if getText(name.childNodes) == "workspace":
...

with getText as defined on the webpage referenced above.

Kind regards,
Dobedani


http://www.python.org/doc/2.4/lib/dom-example.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top