Empty XML element handling in Python -
I'm surprised by the empty element's midiom parser handling, as shown in the following code section.
import xml.dom.minidom doc = xml.dom.minidom.parseString (' & lt; / value & gt;') print doc.firstChild.nodeValue .__ repr __ () # Out: Any print doc.firstChild Toxml () # Out: & lt; Value / & gt; Doc = xml.dom.minidom.Document () v = doc.appendChild (doc.createElement ('value')) v.appendChild (doc.createTextNode ('')) print v.firstChild.nodeValue.__ repr __ () # Out: '' print doc.firstChild.toxml () # Out: & lt; Value & gt; & Lt; / Value & gt;
How do I get consistent behavior? I want to get the empty string as the value of the empty element (which I which I placed in the first place in XML structure) .
Searching for open xml.dom.minidom cracking and "/>", we find:
Method of # Element (node) class. Def writexml (self, author, indent = "", addindent = "", newl = ""): # [snip] if node in self.childNodes: author.write (">% s"% (newl)) Self.childNodes for: node.writexml (author, indent + addindent, addindent, newl) Writer. Written ("% s % s>% s"% (indent, sitename, niel)) and: Author. "/"> <% S "% (newl))
We can detect that the short-end-tag form occurs when childNodes are an empty list, in fact, This is happening:
> doc = document ()> & gt;>> V = doc.createChild (doc.createElement ( 'V'))> gt; & gt; V.toxml () '& lt; v / & gt; & Gt;> gt; V.childNodes []> gt; & gt; V.appendChild (doc.createTextNode ('')) & lt; DOM Text node "'" & gt; & Gt; & Gt; & Gt; V.childNodes [& lt; DOM Text node "'" "& gt;]> gt;>> V.toxml ()' gt; & lt; / v & gt;
As told by Lloyd, the XML tool does not make any difference between the two if your code does does , it means that you will again Think about how you want to serial your data
xml.dom.minidom displays in just a few different ways because it is easy to code however, you can get consistent output. Just get the element
square and toxml
Override the policy, as it will print end-end-tag form when there are no nodes with non-empty text content, then the port is portspatch for using its new element class.
Comments
Post a Comment