Search code examples
pythonxmlminidom

Nested XML tags in Python


I have a nested XML that looks like this:

<data>foo <data1>hello</data1> bar</data>

I am using minidom, but no matter how I try to get the values between "data", I am only get "foo" but not "bar"

It is even worse if the XML is like this:

<data><data1>hello</data1> bar</data>

I only get a "None", which is correct according to the logic above. So I came accross this: http://levdev.wordpress.com/2011/07/29/get-xml-element-value-in-python-using-minidom and concluded that it is due to the limitation of minidom?

So I used the method in that blog and I now get

foo <data1>hello</data1> bar

and

<data1>hello</data1> bar

which is acceptable. However, if I try to create a new node (createTextNode) using the output above as node values, the XML becomes:

<data>foo &lt;data1&gt;hello&lt;/data1&gt; bar</data>

and

<data>&lt;data1&gt;hello&lt;/data1&gt; bar</data>

Is there any way that I can create it so that it looks like the original? Thank you.


Solution

  • So after pointed out by @pandubear, the XML:

    <data>foo <data1>hello</data1> bar</data>
    

    Does have two text nodes, containing "foo " and " bar", so what can be done is to iterate through all the child nodes in data and get the values.