简体   繁体   中英

python object to xml hierarchy

I have a set of xml files that are kind of defined in a tree like structure. The structure is somewhat like:

root -> view[{ config, c_header, a_header }]

I would like to be able to define all this in a set of python objects and define a method that O can could to introspect itself and generate all the xml on the fly, depending on the locally instantiated data. Sorry if I'm not explaining myself very clearly, but I have very little background in OOP and Design Patterns, while I know this is possible, I just can't seem to explain exactly. What I'm trying to find out is some guidelines on how to approach a project/architecture like this in a way that is purely OO driven and makes use of the adequate design patterns. Any advice on what patterns do I need to investigate, examples of similar projects, etc, would be very welcome too!

Well you could use something like Etree , LXML Etree , or beautiful soup etc. these can do this and more. However sometimes you may want to add some implementation details in that isn't practical with any of those so there's something like the ETreeFactory that you can implement yourself or at least understand how you could go ahead with this. The example is not particularly well done but should give you a hint.

class XmlMixin(list):
    """
    simple class that provides rudimentary
    xml serialisation capabiities. The class 
    class uses attribute child that is a list
    for recursing the structure.
    """
    def __init__(self, *children):
        list.__init__(self, children)

    def to_xml(self):
        data = '<%(tag)s>%(internal)s</%(tag)s>'
        tag = self.__class__.__name__.lower()
        internal = ''
        for child in self:
            try:
                internal += child.to_xml()
            except:
                internal += str(child)
        return data % locals()


# some example classes these could have 
# some implementation details
class Root(XmlMixin):
    pass

class View(XmlMixin):
    pass

class Config(XmlMixin):
    pass

class A_Header(XmlMixin):
    pass


root =  Root(
            View(
                Config('my config'),
                A_Header('cool stuff')
            )
       )

#add stuff to the hierarchy
root.append( 
        View(
           Config('other config'),
           A_Header('not so cool')
           )
        )

print root.to_xml()

But like i said use some of the library functions instead then you don't nee do implement so much and you get a reader too. Tough implementing a from_xml isn't hard either.

Update: changed the class to inherit from list. This makes it nicer add/remove elements form within the tree. Added example showing how to expand the tree after initial creation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM