Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: XML

Thread Tools


Stuart Bishop
Posts: n/a
Hash: SHA1

On Tuesday, June 24, 2003, at 02:49 AM, Roman Suzi wrote:

> ------------------
> foo = "123"
> bar = "456"
> zoo = "la\"lala"
> ------------------
> And it's not very hard to parse that.
> In case of XML I will need something like
> <?xml version="1.0"?>
> <foo>123</foo><bar>456</bar><zoo>la&quot;lala</zoo>

> - not a big deal, but it's harder to parse. And also XML software keeps
> changing (or so it seems), and this gives a sense of instability.

Are you just assuming this? The following works happily under
both Python2.1.3 and Python2.2.3, and probably every version
since minidom appeared.

import xml.dom.minidom
import pprint

my_xml = '''
<set name="foo">123</set>
<set name="bar">456</set>
<set name="zoo">la"lala</set>
<set name="baz">Rene&#xe9; or Rene\xc3\xa9</set>
<set name="quux">"""oops="moo"</set>
''' # UTF8 by defailt, or add a <?xml encoding="foo"?> for other

settings = {}
d = xml.dom.minidom.parseString(my_xml)
for set_node in d.getElementsByTagName('set'):
name = set_node.getAttribute('name')
value = [t.wholeText for t in set_node.childNodes]
settings[name] = value

> XML always gives me a feeling that I do not fully master it
> (especially it's
> DTD part)! And this is after two years of trying to understand it.
> (Cf: with
> Python felt at home after a week or two!)

XML gets pretty hairy, generally when you are trying to *do* something
hairy. If you can program Python, you probably have no need for XSLT.
DTD's and schemas are only needed if you need to validate your data.
The advantage is, that these tools and many more are available if you
*do* need them.

XML can also be dead easy if you don't get carried away.
Using a better interface than the DOM (Elementree or pyRXP)
makes it even easier, since you end up with nice pythonic
lists'n'stuff to deal with instead of having to lookup method
names like getElementsByTagName or wholeText.

> P.S. Just look at the neighboor thread:
> Subject: minidom toxml() not emitting attribute namespace qualifier

I notice that your example code isn't emitting attribute namespace
qualifier's either One more XML feature that you probably never
have to worry about, if you are able to use as simplistic a format as
you describe above. I notice elsewhere in this thread that this bug has
also been fixed by someone already, whereas if there is a bug in your
parsing code *you* would have had to fix it.

- --
Stuart Bishop <(E-Mail Removed)>

Version: GnuPG v1.2.1 (Darwin)


Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Different results parsing a XML file with XML::Simple (XML::Sax vs. XML::Parser) Erik Wasser Perl Misc 5 03-05-2006 10:09 PM
Print XML parsing to JspWriter (out) Class org.xml.sax.helpers.NewInstance can not access a member of class javax.xml.parsers.SAXParser with modifiers "protected" Per Magnus L?vold Java 0 11-15-2004 02:27 PM
embedding xml in xml as non-xml :) Mark Van Orman XML 5 09-15-2004 05:57 AM
What XML technologies to learn first for "XML Processing" and "XML Mapping"? Bomb Diggy Java 0 07-28-2004 07:26 AM
Help on including one XML document within another XML document using XML Schemas Tony Prichard XML 0 12-12-2003 03:18 PM