diff options
| author | David Sommerseth <davids@redhat.com> | 2009-07-29 20:22:24 +0200 |
|---|---|---|
| committer | David Sommerseth <davids@redhat.com> | 2009-07-29 20:22:24 +0200 |
| commit | e5e8141637a3b5f3eb7728cd9d794aa2beb8e270 (patch) | |
| tree | dfc99a8dc7896cb487c6833da61b8a8999cece58 /server/xmlparser.py | |
| parent | 72f3ff701811ae6263627b94bb65a4b5f13dc526 (diff) | |
Improved xmlparser.py/XMLSQLparser class to handle libxml2.xmlDoc and libxml2.xmlNode
Valid input for both the XSLT and XML input parameters in the constructor
now can parse libxml2.xmlDoc, libxml2.xmlNode or a string containing a filename
Diffstat (limited to 'server/xmlparser.py')
| -rw-r--r-- | server/xmlparser.py | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/server/xmlparser.py b/server/xmlparser.py index ec3bb08..a476f49 100644 --- a/server/xmlparser.py +++ b/server/xmlparser.py @@ -29,12 +29,13 @@ import libxml2 import libxslt import hashlib import StringIO +import types class XMLSQLparser(object): "Class for parsing XML into SQL using an XSLT template for mapping data fields" - def __init__(self, xslt, fname): - self.xml = libxml2.parseFile(fname) + def __init__(self, xslt, xml): + self.xml = self.__get_xml_data(xml) # Verify that this is a valid rteval XML file try: @@ -44,9 +45,27 @@ class XMLSQLparser(object): except Exception, err: raise Exception, 'Input file was unparsable or not a valid rteval XML file (%s)' % str(err) - xsltdoc = libxml2.parseFile(xslt) + xsltdoc = self.__get_xml_data(xslt) self.parser = libxslt.parseStylesheetDoc(xsltdoc) + def __get_xml_data(self, input): + if hasattr(input, '__module__') and (input.__module__ == 'libxml2') and hasattr(input, 'get_type'): + if input.get_type() == 'document_xml': + # It's an XML document, use it directly + return input + elif input.get_type() == 'element': + # It's an XML node, create a document and set node as root + xmldoc = libxml2.newDoc("1.0") + xmldoc.setRootElement(input) + return xmldoc + elif type(input) == types.StringType: + # It's a string, assume a file name + return libxml2.parseFile(input) + + # If invalid input ... + raise AttributeError, "Unknown input type for XML/XSLT data (not a filename, xmlDoc or xmlNode)" + + def __xmlNode2string(self, node): doc = libxml2.newDoc('1.0') doc.setRootElement(node) |
