By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,603 Members | 1,000 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,603 IT Pros & Developers. It's quick & easy.

ISO dict => xml converter

P: n/a
kj


Hi. Does anyone know of a module that will take a suitable Python
dictionary and return the corresponding XML structure?

In Perl I use XML::Simple's handy XMLout function:

use XML::Simple 'XMLout';
my %h = ( 'Foo' =+{
'Bar' =+{
'Baz' =[ { 'meenie' =3 },
{ 'meenie' =7 } ],
'eenie' =4,
},
'minie' =1,
'moe' =2,
} );

print XMLout( \%h, KeepRoot =1, KeyAttr =undef );
__END__
<Foo minie="1" moe="2">
<Bar eenie="4">
<Baz meenie="3" />
<Baz meenie="7" />
</Bar>
</Foo>

Is there a Python module that can do a similar conversion from
a Python dict to an XML string?

(FWIW, I'm familiar with xml.marshal.generic.dumps, but it does
not produce an output anywhere similar to the one illustrated
above.)

TIA!

Kynn

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies

P: n/a
kj wrote:
Hi. Does anyone know of a module that will take a suitable Python
dictionary and return the corresponding XML structure?

In Perl I use XML::Simple's handy XMLout function:

use XML::Simple 'XMLout';
my %h = ( 'Foo' =+{
'Bar' =+{
'Baz' =[ { 'meenie' =3 },
{ 'meenie' =7 } ],
'eenie' =4,
},
'minie' =1,
'moe' =2,
} );

print XMLout( \%h, KeepRoot =1, KeyAttr =undef );
__END__
<Foo minie="1" moe="2">
<Bar eenie="4">
<Baz meenie="3" />
<Baz meenie="7" />
</Bar>
</Foo>

Is there a Python module that can do a similar conversion from
a Python dict to an XML string?
This is so trivial to do in ElementTree that I wouldn't expect there to be a
special package for this. If you write the estimated 15 lines of code
yourself, you can even tweak it into exactly the structure you want.

Stefan
Jun 27 '08 #2

P: n/a
On Jun 20, 1:37*pm, kj <so...@987jk.com.invalidwrote:
Hi. *Does anyone know of a module that will take a suitable Python
dictionary and return the corresponding XML structure?

In Perl I use XML::Simple's handy XMLout function:

* use XML::Simple 'XMLout';
* my %h = ( 'Foo' =+{
* * * * * * * * * * * * 'Bar' =+{
* * * * * * * * * * * * * * * * * * 'Baz' =[ { 'meenie' =3 },
* * * * * * * * * * * * * * * * * * * * * * * *{ 'meenie' =7 } ],
* * * * * * * * * * * * * * * * * * 'eenie' =4,
* * * * * * * * * * * * * * * * * },
* * * * * * * * * * * * 'minie' =1,
* * * * * * * * * * * * 'moe' =2,
* * * * * * * * * * * } );

* print XMLout( \%h, KeepRoot =1, KeyAttr =undef );
* __END__
<Foo minie="1" moe="2">
* <Bar eenie="4">
* * <Baz meenie="3" />
* * <Baz meenie="7" />
* </Bar>
</Foo>

Is there a Python module that can do a similar conversion from
a Python dict to an XML string?

(FWIW, I'm familiar with xml.marshal.generic.dumps, but it does
not produce an output anywhere similar to the one illustrated
above.)

TIA!

Kynn

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
Try:
http://pyxml.sourceforge.net/topics/howto/node26.html

- Paddy.
Jun 27 '08 #3

P: n/a
On Jun 20, 6:37*am, kj <so...@987jk.com.invalidwrote:
Hi. *Does anyone know of a module that will take a suitable Python
dictionary and return the corresponding XML structure?

In Perl I use XML::Simple's handy XMLout function:

* use XML::Simple 'XMLout';
* my %h = ( 'Foo' =+{
* * * * * * * * * * * * 'Bar' =+{
* * * * * * * * * * * * * * * * * * 'Baz' =[ { 'meenie' =3 },
* * * * * * * * * * * * * * * * * * * * * * * *{ 'meenie' =7 } ],
* * * * * * * * * * * * * * * * * * 'eenie' =4,
* * * * * * * * * * * * * * * * * },
* * * * * * * * * * * * 'minie' =1,
* * * * * * * * * * * * 'moe' =2,
* * * * * * * * * * * } );

* print XMLout( \%h, KeepRoot =1, KeyAttr =undef );
* __END__
<Foo minie="1" moe="2">
* <Bar eenie="4">
* * <Baz meenie="3" />
* * <Baz meenie="7" />
* </Bar>
</Foo>

Is there a Python module that can do a similar conversion from
a Python dict to an XML string?

(FWIW, I'm familiar with xml.marshal.generic.dumps, but it does
not produce an output anywhere similar to the one illustrated
above.)
What about

-----------------------------------------
import lxml.etree as ET
from lxml.builder import E

h = E.Foo(
dict(minie='1', moe='2'),
E.Bar(
dict(eenie='4'),
E.Baz(meenie='3'),
E.Baz(meenie='7')))

print ET.tostring(h, pretty_print=True)

----------------------------------------
<Foo moe="2" minie="1">
<Bar eenie="4">
<Baz meenie="3"/>
<Baz meenie="7"/>
</Bar>
</Foo>
---------------------------------------

Waldemar

Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.