Need to parse python dictionaries into xml
abhijeet thatte
abhijeet.thatte at gmail.com
Wed Jun 16 14:41:01 EDT 2010
On Wed, Jun 16, 2010 at 10:57 AM, Stefan Behnel <stefan_ml at behnel.de> wrote:
> Hi, please avoid top-posting.
>
> abhijeet thatte, 16.06.2010 18:46:
>
> On Tue, Jun 15, 2010 at 11:09 PM, Stefan Behnel wrote:
>>
>>> abhijeet thatte, 16.06.2010 03:05:
>>>
>>>> I am a novice Python user. I am using Python to parse some hardware
>>>> specifications and create xml files from them.
>>>> I generate dict of really huge sizes. (I am parsing some 10,000 register
>>>> definitions.)
>>>>
>>>
>>> Why do you need these intermediate dicts?
>>>
>>> So, it looks like : {elem1,elem2, elem3,dict1,{elem4,elem5, dict2
>>>> {elem6,
>>>> elem7, dict3{.....}}}}.
>>>>
>>>>
>>> What is this hierarchy of dicts mapping to what?
>>>
>>> It's always best to provide a short extract of a real example rather than
>>> something you made up.
>>>
>> >
>
>> I am parsing some hardware format which finally I need to convert in xml.
>>
>> The intermediate step is dicts.
>> So, the structure looks like this:
>>
>> {chip_name:'myChip',chip_clock:'3.07',chip_peripherals:{peripheral1:{mode:'mode1',register:{}},peripheral2:{....},peripheral3:{...}}}
>>
>
> With "real example" I meant real code and real (or close to real) data. The
> above is not a valid Python dictionary. I assume the keys are supposed to be
> strings?
>
>
>
> I think this example gives good insight into kind of dict structure I am
>> having. Now I want to have every dict tag as xml tag with hierarchy
>> maintained.
>>
>
> What is name of the root element that you want?
>
> You should start by writing down the XML structure that you want to build
> for the above dict. That will make it clear what needs to be done.
Stefan
>
> /*****************************************************************************************/
>
I need an xml file structure as below:
<chipsim>
<#>
<name>sh2a</name>
<size>32</size>
<bus_width>4</bus_width>
<#><PIF>
<name>PIF</name>
<offset>4</offset>
<name_of_peer_string>new_string</name_of_peer_string>
</PIF>
<#><interrupt_tree>
<name>Interrupt_tree</name>
<#><tree_level_0>
<name>MX</name>
<offset>32</offset>
<bit_position>1</bit_position>
<type>INTR</type>
<hi_mask_def>0000</hi_mask_def>
<lo_mask_def>0000</lo_mask_def>
<#><tree_level_1>
<name>RRPR</name>
<offset>928</offset>
<bit_position>0</bit_position>
<type>INTR</type>
<hi_mask_def>0000</hi_mask_def>
<lo_mask_def>0000</lo_mask_def>
</tree_level_1>
</tree_level_0>
</interrupt_tree>
</chipsim>
//**************************************************************************************/
This is a very small part of the actual output I need.
Dicts which I am creating looks like =
{'chipsim':{'name':sh2a,'size':32,'bus_width':'4','PIF':{'name':'PIF','offseet':'4','name_of_peer_string':'new_string'},'interrupt_tree':{'tree_level_0':{'tree_level_1':{.....}}}}}
> I guess ElementTree is the best way to go about it. But have not found a
> good reference on how to use it.
>
> Thanks
Abhijeet
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100616/e75bd307/attachment-0001.html>
More information about the Python-list
mailing list