使用属性的Python XML序列化

时间:2013-08-16 15:33:10

标签: python xml serialization xml-serialization

我试图将python中的(几个)复杂数据结构序列化为非常明确的XML字符串。

在C#中,这就像创建数据结构一样简单,使用某些属性标记字段,如[XmlElement]或[XmlAttribute],并基本上调用“serialise”。

但是,我无法在python中找到类似的功能。我可以看到大量手动解析结构的例子,但这并不适合我的需要。

无论如何都要模拟这个C#功能;

public enum eType {

    [XmlEnum("multi")]
    Multiple,

    [XmlEnum("mutex1")]
    Single,

    [XmlEnum("product")]
    Product,

    [XmlEnum("alias")]
    Alias
}

[Serializable]
[XmlRoot("root")]
public class RootClass{

    public RootClass() {
        Metadata = new Metadata ();
        FeatureDictionary = new FeatureDictionary ();
    }

    [XmlElement("metadata")]
    public Metadata Metadata { get; set; }

    [XmlElement("feature-dictionary")]
    public FeatureDictionary FeatureDictionary { get; set; }

}

[Serializable]
public class Metadata {

    public Metadata() {
        Meta = new List<Meta> ();
    }

    [XmlAttribute("status")]
    public string Status { get; set; }

    [XmlAttribute("url")]
    public string URL { get; set; }

    [XmlAttribute("view")]
    public string View { get; set; }

    [XmlElement("meta")]
    public List<Meta> Meta { get; set; }

}

在python中?

请注意,上面的代码段大约是在C#中定义XML的代码的1/20。

1 个答案:

答案 0 :(得分:2)

一种合理的方法是使用python descriptors在对象上创建属性,这些属性知道如何序列化和反序列化自身。描述符是python用于创建@property装饰器的机制:包含getter和setter方法,并且可以具有本地状态,因此它们可以在数据和xml之间建立良好的分段。与一个类或装饰器相结合,可以自动化批量序列化/反序列化附加到对象的描述符的过程,您就拥有了C#XML序列化系统的内涵。

通常,您希望代码看起来像这样(使用臭名昭着的XML ISBN示例:

 @xmlobject("Book")  
 class Book( object  ):

    author = XElement( 'AuthorsText' )
    title = XElement( 'Title' )
    bookId = XAttrib( 'book_id' )
    isbn = IntAttrib( 'isbn' )
    publisher = XInstance( 'PublisherText', Publisher )

这里的分配语法是为实例中的所有字段(作者,标题等)创建类级描述符。每个描述符看起来像是其他python代码的常规字段,因此您可以执行以下操作:

book.author = 'Joyce, James'

等等。在内部,每个描述符都存储和xml节点或属性,并且当被调用以进行序列化时,它将返回相应的XML:

from xml.etree.cElementTree import ElementTree, Element

class XElement( object ):
    '''
    Simple XML serializable field
    '''

    def __init__( self, path):           
        self.path = path
        self._xml = Element(path) # using an ElementTree or lxml element as internal storage

    def get_xml( self, inst ):
        return inst._xml

    def _get_element( self ):
        return self.path

    def _get_attribute( self ):
        return None

    # the getter and setter push values into the underlying xml and return them from there
    def __get__( self, instance, owner=None ):
         myxml = self.get_xml( instance )
         underlying = myxml.find( self.path )
         return underlying.text 

    def __set__( self, instance, value, owner=None ):
        myxml= self._get_xml( instance )
        underlying = myxml.find( self.path )
        underlying.text = value

相应的XAttrib类执行相同的操作,除了属性而不是元素。

class XAttrib( XElement):
    '''
     Wraps a property in an attribute on the containing xml tag specified by 'path'
    '''

    def __get__( self, instance, owner=None ):
        return self._get_xml( instance ).attrib[self.path]  
        # again, using ElementTree under the hood

    def __set__( self, instance, value, owner=None ):
        myxml = self._get_xml( instance )
        has_element = myxml.get( self.path, 'NOT_FOUND' )
        if has_element == 'NOT_FOUND':
           raise Exception, "instance has no element path"
        myxml.set( self.path, value )

    def _get_element( self ):
        return None  #so outside code knows we are an attrib

    def _get_attribute( self ):
        return self.path

为了将它们组合在一起,拥有类需要在初始化时设置描述符,因此每个实例级描述符都指向拥有实例自己的XML元素中的XML节点。这样,对实例道具的更改会自动反映在所有者的XML中。

        def create_defaults( target_cls):
             # where target class is the serializable class, eg 'Book'
             # here _et_xml() would return the class level Element, just
             # as in the XElement and XAttribute.  Good use for a decorator!

             myxml = target_cls.get_xml()

             default_attribs = [item for item in target_cls.__class__.__dict__.values() 
                                 if issubclass( item.__class__, XElement) ]
             #default attribs will be all the descriptors in the target class

             for item in default_attribs:
                element_name = item._get_element()
                #update the xml for the owning class with 
                # all the XElements
                if element_name:
                    new_element = Element( element_name )
                    new_element.text = str( item.DEFAULT_VAL )
                    myxml.append( new_element )

                # then update the owning XML with the attributes 
             for item in default_attribs:
                 attribpath = item._get_attribute()
                 if attrib:
                     myxml.set( attribpath, str( item.DEFAULT_VAL ) )

道歉,如果这段代码没有运行 - 我从一个工作示例中删除了它,但我可能在尝试使其可读并删除特定于我的应用程序的详细信息时引入了错误。