XML to dictionary?
jim kraai
jkraai at murl.com
Thu Jan 6 17:09:17 EST 2000
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
More information about the Python-list
mailing list