Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a link to an <a href="http://code.activestate.com/recipes/116539/" rel="nofollow noreferrer">ActiveState solution</a> - and the code in case it disappears again.</p> <pre><code>================================================== xmlreader.py: ================================================== from xml.dom.minidom import parse class NotTextNodeError: pass def getTextFromNode(node): """ scans through all children of node and gathers the text. if node has non-text child-nodes, then NotTextNodeError is raised. """ t = "" for n in node.childNodes: if n.nodeType == n.TEXT_NODE: t += n.nodeValue else: raise NotTextNodeError return t def nodeToDic(node): """ nodeToDic() scans through the children of node and makes a dictionary from the content. three cases are differentiated: - if the node contains no other nodes, it is a text-node and {nodeName:text} is merged into the dictionary. - if the node has the attribute "method" set to "true", then it's children will be appended to a list and this list is merged to the dictionary in the form: {nodeName:list}. - else, nodeToDic() will call itself recursively on the nodes children (merging {nodeName:nodeToDic()} to the dictionary). """ dic = {} for n in node.childNodes: if n.nodeType != n.ELEMENT_NODE: continue if n.getAttribute("multiple") == "true": # node with multiple children: # put them in a list l = [] for c in n.childNodes: if c.nodeType != n.ELEMENT_NODE: continue l.append(nodeToDic(c)) dic.update({n.nodeName:l}) continue try: text = getTextFromNode(n) except NotTextNodeError: # 'normal' node dic.update({n.nodeName:nodeToDic(n)}) continue # text node dic.update({n.nodeName:text}) continue return dic def readConfig(filename): dom = parse(filename) return nodeToDic(dom) def test(): dic = readConfig("sample.xml") print dic["Config"]["Name"] print for item in dic["Config"]["Items"]: print "Item's Name:", item["Name"] print "Item's Value:", item["Value"] test() ================================================== sample.xml: ================================================== &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;Config&gt; &lt;Name&gt;My Config File&lt;/Name&gt; &lt;Items multiple="true"&gt; &lt;Item&gt; &lt;Name&gt;First Item&lt;/Name&gt; &lt;Value&gt;Value 1&lt;/Value&gt; &lt;/Item&gt; &lt;Item&gt; &lt;Name&gt;Second Item&lt;/Name&gt; &lt;Value&gt;Value 2&lt;/Value&gt; &lt;/Item&gt; &lt;/Items&gt; &lt;/Config&gt; ================================================== output: ================================================== My Config File Item's Name: First Item Item's Value: Value 1 Item's Name: Second Item Item's Value: Value 2 </code></pre>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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