473,386 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

ISO dict => xml converter

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
3 1647
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jon Rea | last post by:
is there a c# -> c++ code converter out there ? Hi people, I was just wondering if there is a C# to C++ converter out there. I have some working c# code and need to integrate it into a...
4
by: matty.hall | last post by:
I have two classes: a base class (BaseClass) and a class deriving from it (DerivedClass). I have a List<DerivedClass> that for various reasons needs to be of that type, and not a List<BaseClass>....
6
by: karch | last post by:
I am beginning to write my first applications with C++/CLI and was wondering if someone could demonstrate the proper way to convert a sample piece of code. I just need some help understanding the...
7
by: karch | last post by:
Thanks to everyone who has helped with some of my elementary questions about C++/CLI equivalents to C#. I have a few more items that are giving me headaches. All help is appreciated. What is the...
2
by: Sam | last post by:
Hi, I've seen this kind of thing in C# quite often : cbo.Validating += new CancelEventHandler(cbo_Validating); what is the vb.net equivalent ? Please don't send me to...
2
by: Maury | last post by:
Do you know if somewhere exists the conversione of Vb.Net 'My' class (or better My.Computer.Network.Ping) in C Sharp? Thanks M.A.
4
by: Andrew | last post by:
One year ago I have programmed in VB. There was a function named "InStrRev". In C# I don't have found a similar function. Can anybody help me in this question. Thanks a lot regards andrew
21
by: py_genetic | last post by:
Hello, I'm importing large text files of data using csv. I would like to add some more auto sensing abilities. I'm considing sampling the data file and doing some fuzzy logic scoring on the...
4
by: Soren | last post by:
Hi, I want to control some motors using the parallel port.. however, my laptop does not have any parallel ports (very few do). What I do have is a USB->Parallel converter... I thought about...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.