Author: bugman Date: Sat Jun 28 20:36:59 2008 New Revision: 6527 URL: http://svn.gna.org/viewcvs/relax?rev=6527&view=rev Log: Created xml_to_object() to place the contents of an XML element into a python container. Modified: 1.3/data/relax_xml.py Modified: 1.3/data/relax_xml.py URL: http://svn.gna.org/viewcvs/relax/1.3/data/relax_xml.py?rev=6527&r1=6526&r2=6527&view=diff ============================================================================== --- 1.3/data/relax_xml.py (original) +++ 1.3/data/relax_xml.py Sat Jun 28 20:36:59 2008 @@ -25,6 +25,7 @@ # Python module imports. from re import search +from string import strip def fill_object_contents(doc, elem, object=None, blacklist=None): @@ -61,3 +62,30 @@ # Add the text value to the sub element. text_val = doc.createTextNode(`getattr(object, name)`) sub_elem.appendChild(text_val) + + +def xml_to_object(elem, base_object=None): + """Convert the XML elements into python objects, and place these into the base object. + + @param elem: The element to extract all python objects from. + @type elem: xml.dom.minidom.Element instance + @param base_object: The python class instance to place the objects into. + @type base_bject: instance + """ + + # Loop over the nodes of the element + for node in elem.childNodes: + # Skip empty nodes. + if node.localName == None: + continue + + # The name of the python object to recreate. + name = str(node.localName) + + # Get the node contents. + val = eval(strip(node.childNodes[0].nodeValue)) + + # Set the value. + setattr(base_object, name, val) + +