Ejemplo n.º 1
0
def show(xml_src, parser):
    """Self test using simple or user-specified XML data
    >>> xml = '''
    ...
    ...
    ...    Some text about eggs.
    ...    Ode to Spam
    ...  '''
    >>> squeeze = lambda s: s.replace(LF*2,LF).strip()
    >>> print squeeze(show(xml,DOM)[0])
    -----* _XO_Spam *-----
    {Eggs}
       PCDATA=Some text about eggs.
    {MoreSpam}
       PCDATA=Ode to Spam
    >>> print squeeze(show(xml,EXPAT)[0])
    -----* _XO_Spam *-----
    {Eggs}
       PCDATA=Some text about eggs.
    {MoreSpam}
       PCDATA=Ode to Spam
    PCDATA=
    """
    try:
        xml_obj = XML_Objectify(xml_src, parser=parser)
        py_obj = xml_obj.make_instance()
        return (pyobj_printer(py_obj).encode('UTF-8'),
                "++ SUCCESS (using " + parser + ")\n")
    except:
        return ("", "++ FAILED (using " + parser + ")\n")
Ejemplo n.º 2
0
    for instance_name in type_dict['instance']:
        instance = getattr(object, instance_name)
        descript = descript + (' '*level)+'<'+instance_name
        # look ahead to see if the next instance contains lists or instances
        nested_type_dict = _get_type_dict(instance)
        descript = descript + XML_printer(instance, level+INDENT,
                                          nested_type_dict)
        if nested_type_dict['instance'] or nested_type_dict['list']:
            descript = descript + (' '*level)+'</'+instance_name+'>\n'

    for list_name in type_dict['list']:
        inst_list = getattr(object, list_name)
        for instance in inst_list:
            descript = descript + (' '*level)+'<'+list_name
            nested_type_dict = _get_type_dict(instance)
            descript = descript + XML_printer(instance, level+INDENT,
                                              nested_type_dict)
            if nested_type_dict['instance'] or nested_type_dict['list']:
                descript = descript + (' '*level)+'</'+list_name+'>\n'

    return  descript


if __name__ == '__main__':
    import sys
    from gnosis.xml.objectify import XML_Objectify, EXPAT
    xml_obj = XML_Objectify(sys.argv[1], EXPAT)
    pyobj = xml_obj.make_instance()
    print XML_printer(pyobj)

Ejemplo n.º 3
0
import sys
from gnosis.xml.objectify import XML_Objectify, pyobj_printer, EXPAT


class MyExpatBased_XML_Objectify(XML_Objectify):
    expat_args = []
    expat_kwargs = {"nspace_sep": None}  # Ignore XML namespaces.

    def __init__(self, xml_src):
        XML_Objectify.__init__(self, xml_src=xml_src, parser=EXPAT)


if len(sys.argv) > 1:
    for filename in sys.argv[1:]:
        try:
            xml_obj = XML_Objectify(xml_src=filename, parser=EXPAT)
            py_obj = xml_obj.make_instance()
        except SyntaxError:
            print """Caught SyntaxError exception! Possibly an XML file with namespaces that
is causing this, so try again but ignore XML namespaces...""",
            xml_obj = MyExpatBased_XML_Objectify(filename)
            try:
                py_obj = xml_obj.make_instance()
                print "it worked!"
            except:
                print "it did NOT work!"
                raise
        print pyobj_printer(py_obj).encode("UTF-8")
else:
    print "Please specify one or more XML files to Objectify."