SourceForge.net Logo Support This Project

XMLData v1.0


There are some great python XML packages out there (many built on top of the minidom or sax that now comes with python) but I wanted something really simple. Only 228 lines of code, XMLData provides 90% of the functionality required to de/seralize python objects to/from XML. It is particularly suited for OPML, RSS and XML based configuration files.

Download


The latest package is available on sourceforge: XMLData-1.0
You can browse source here.

License


XMLData v1.0 is released under the LGPL. You can use it in your software and process templates without having to release your software or templates under any particular license. If you distribute PYT with or without modification you must make source code available and release it under a compatible license as per the LGPL. A copy of the LGPL is available at http://www.gnu.org/copyleft/lesser.html

Getting Started


XMLData load functions take a file or XML string and return a python object. In general, nested tags appear as data members, while lists of same named sibling takes appear as lists and attributes are dict members (e.g. tag[atttribute_name] = attribute_value).

API


from XMLData import *

#getting a minidom node
ParseAsXML(xml_string) -> xml minidom node
LoadXML(xmlFile) -> xml minidom node

#getting an XMLData node
ParseAsXMLData(xml_string) -> XMLData node
LoadXMLData(xmlFile) -> XMLData node

#save an XMLData
SaveXMLData(xmldata, xmlFile)

#get a list of xmldata nodes even if there's only one
NodeList(node_or_list) -> list of XMLData nodes

#get a map of xmldata nodes based on an immediate attribute or value
#note - if two nodes have the same key, only one is returned
#	for more advanced processing see find
NodeMap(node, attributeKey) -> map of XMLData nodes
"""
#example of using xmldata with an rss feed
xml = ParseAsXMLData("""

  
    Sample Feed
    sample.feed.url
    Here's a sample feed for use with XMLData
    en-us
    
      First Item
      sample.feed.url/first
      This is the first item in our sample feed.
    
    
      Second Item
      sample.feed.url/second
      This is the second item in our sample feed.
    
  

""")

print xml['version']
#2.0

print xml.channel.title
#Sample Feed

print len(xml.channel.item)
#2

print xml.find("/channel/item[title = First Item]")[0].link
#sample.feed.url/first