anijhaw
anijhaw

Reputation: 9422

Serialize a message format to xml

I have a python list as

 [
     (A,{'a':1,'b':2,'c':3,'d':4}),
     B,{'a':1,'b':2,'c':3,'d':4}),
     ...
     ]

I want to know if there is a standard library of serializing this kind of list to xml or should I hand code it to a file.

Edit : Added Detail

Assuming this is used to construct a message such that

message = A( Feild attributes{'a':1,'b':2,'c':3,'d':4}) || B Field attributes{'a':1,'b':2,'c':3,'d':4}) || C Field attributes{'a':1,'b':2,'c':3,'d':4})

Upvotes: 0

Views: 382

Answers (3)

makapuf
makapuf

Reputation: 1410

"use json/yaml/whitespace" comments aside (I suppose you have your reasons to do so, instead go for pickle/json),

you can try the very pythonic elementtree library (in the standardlib), or even use some advice from google : search "converting python dictionary to xml"

(not to sound too rude .. take it with a wink)

looking at your example, what are A and B ? integers ? strings ? classmethods ?

Upvotes: 2

Mike Graham
Mike Graham

Reputation: 76753

  • Why are you using XML? There are often better solutions, like JSON, which is plenty portable and standard.
  • The easiest way might be to use YAML. YAML's main representation is not XML, but there is a canonical way (YAXML) to represent YAML serialized data as XML.

Upvotes: 1

Mike DeSimone
Mike DeSimone

Reputation: 42825

Does it need to be XML? This is the usual domain of the pickle module.

But, no, there's no standard serialize-Python-object-to-XML library. (I have one I wrote a while ago, it's not published, much less "standard".) There are libraries like lxml for converting XML to Python objects and back, and the usual sax, dom, and expat libraries for reading XML.

Upvotes: 4

Related Questions