Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to parse strings representing xml.dom.minidom nodes in python?
    primarykey
    data
    text
    <p>I have a collection of nodes xml.dom.Node objects created using xml.dom.minidom. I store them (individually) in a database by converting them to a string using the toxml() method of a the Node object.</p> <p>The problem is that I'd sometimes like to be able to convert them back to the appropriate Node object using a parser of some kind. As far as I can see the various libraries shipped with python use Expat which won't parse a string like '' or indeed anything which is not a correct xml string.</p> <p>So, does anyone have any ideas? I realise I could pickle the nodes in some way and then unpickle them, but that feels unpleasant and I'd much rather be storing in a form I can read for maintenance purposes. Surely there is something that will do this?</p> <p>In response to the doubt expressed that this is possible, an example of what I mean:</p> <pre><code>&gt;&gt;&gt; import xml.dom.minidom &gt;&gt;&gt; x=xml.dom.minidom.parseString('&lt;a&gt;foo&lt;b&gt;thing&lt;/b&gt;&lt;/a&gt;') &gt;&gt;&gt; x.documentElement.childNodes[0] &lt;DOM Text node "u'foo'"&gt; &gt;&gt;&gt; x.documentElement.childNodes[0].toxml() u'foo' &gt;&gt;&gt; xml.dom.minidom.parseString(x.documentElement.childNodes[0].toxml()) Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/xml/dom/minidom.py", line 1928, in parseString return expatbuilder.parseString(string) File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/xml/dom/expatbuilder.py", line 940, in parseString return builder.parseString(string) File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/xml/dom/expatbuilder.py", line 223, in parseString parser.Parse(string, True) xml.parsers.expat.ExpatError: syntax error: line 1, column 0 </code></pre> <p>In other words the ".toxml()" method does not create something that Expat (and hence out of the box parseString) will parse. </p> <p>What I would like is something that will parse u'foo' into a text node. I.e. something that will reverse the effect of .toxml()</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload