XML to dictionary?

John Jensen johnjensen at my-deja.com
Thu Jan 6 20:47:11 EST 2000


Yes, I recognize that the keys have to be unique.  The sample I provided isn't a "real" one -- the data I am actually working with is kind of ugly.  In the data I'm working with the child tags are always unique, so it would be great for this kind of solution.

I'm thinking that the best thing to do in this situation is just write a quick customized script, and not worry about using "fancy" libraries like xmllib, pyxie, etc.

JOhn

 
--

On Thu, 06 Jan 2000 16:09:17   jim kraai wrote:
>There's a little problem:
>  dictionary keys must be unique.
>  XML can have more than one child node of the same name.
>
>Depending on the DTD:
>	<Newsgroup>
>	  <comp.lang.python>
>	    <Message>
>	      <Subject>Hi there</Subject>
>	      <Body>This is a test</Body>
>	    </Message>
>	    <Message>
>	      <Subject>Re: Hi there</Subject>
>	      <Body>This is a reply to:\n> This is a test</Body>
>	    </Message>
>	  </comp.lang.python>
>	</Newsgroup>
>is a valid hunk of xml.
>
>If you're controlling this aspect of the DTD, that's fine, but I 
>don't know how you'd write a DTD to encompass:
><Newsgroup>
>	<Message1>...</Message1>
>	<Message2>...</Message2>
>	<Message...>...</Message...>
>	<MessageN>...</MessageN>
></Newsgroup>
>
>This is a 'bad idea' in XML, but then again, I haven't written 
>that many DTDs.
>
>--jim
>
>johnjensen at my-deja.com wrote:
>> 
>> I have a relatively simple XML question, but I can't seem to figure out
>> how to do it simply and easily with the xmllib module.
>> 
>> Assume I have some XML (stylized below):
>> 
>> <Newsgroup>
>>   <comp.lang.python>
>>     <Message1>
>>       <Subject>Hi there</Subject>
>>       <Body>This is a test</Body>
>>     </Message1>
>>   </comp.lang.python>
>> </Newsgroup>
>> 
>> How can I quickly and easily stuff this into a nested dictionary, so
>> that:
>> 
>> print dict['Newsgroup']['comp.lang.python']['Message1']['Subject']
>> spits out 'Hi there'
>> 
>> The XML I am working with is perfect for this type of solution.
>> 
>> Any help would be appreciated!
>> 
>> John
>


--== Sent via Deja.com http://www.deja.com/ ==--
Share what you know. Learn what you don't.




More information about the Python-list mailing list