ANN: XMLRecord 0.1.1
Thomas Weholt
thomas at gatsoft.no
Fri Sep 28 04:32:44 EDT 2001
XMLRecord
Current version: 0.1.1, Alpha
License: Pythonic
Requirements: Python 2.x, pyxie from pyxie (http://pyxie.sourceforge.net )
XMLRecord takes a XML-document describing a record-like structure and turn
it into an object similar to a dataset found in VBScript. It reads the XML
sequentially and parses each "record" into a DOM-object using the
minidom-module. Provided helper-methods makes it easy to extract values of
tags, attributes and list-structures.
XMLRecord is inspired by RAX
http://www.xml.com/pub/a/2000/04/26/rax/index.html ).
It didn't handle lists the way I wanted so I made XMLRecord. Pyxies
simplistic syntax made it easy to parse.
It works on XML-structures like this (info in this xml-piece is fictional. )
:
<doc>
<rec>
<name>Thomas</name>
<age type="int">26</age>
<features>
<feature>short</feature>
<feature>fat</feature>
<feature>dumb</feature>
</features>
<email>thomas at weholt.org</email>
</rec>
<rec>
<name>Roger Wilco</name>
<age type="int" dummy="test">30</age>
<features>
<feature>tall</feature>
<feature>slim</feature>
<feature>smart</feature>
</features>
<email>roger at wilco.com</email>
</rec>
<rec>
<name>Dilbert</name>
<age>32</age>
<features/>
<email/>
</rec>
<rec>
<name>Mr Johnson</name>
<age>47</age>
<features/>
<email>johnson at mail.com</email>
</rec>
</doc>
To use XMLRecord, do something like this ( source edited to fit page ) :
# xml_data is a string containing the XML above
import XMLRecord
x = XMLRecord.XMLRecord('rec', xml_data)
while not x.eof:
print "Name = %s, age = %s" % (x.getTagValue('name'),
x.getTagValue('age'))
x.nextRecord()
The result:
Name = Thomas, age = 26
Name = Roger Wilco, age = 30
Name = Dilbert, age = 32
Name = Mr Johnson, age = 47
XMLRecord also provides a helper-method getListFromTag that constructs a
list of items from a piece of XML like this:
<features>
<feature>short</feature>
<feature>fat</feature>
<feature>dumb</feature>
</features>
into this
['short','fat','dumb']
The tags inside the tag marking up the list don't have to have the same name
as long as the piece of xml is well-formed. One could easily use something
like this :
<features>
<item>short</item>
<something>fat</something>
<foo>dumb</foo>
</features>
The available helper methods so far is:
getAttributeFromTag(self, tag, attr)
getAttributesFromTag(self, tag)
getTagValue(self, tag)
getListFromTag(self, tag)
Tips and comments are very welcome. Source is available at
http://projects.weholt.org
Thomas Weholt,
thomas at weholt.org
More information about the Python-list
mailing list