Created
November 1, 2011 20:55
-
-
Save colin-haber/1331874 to your computer and use it in GitHub Desktop.
Using minidom to get values from XML. Is this really the easiest way to do it?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<plugin> | |
<name>Plugin</name> | |
<version>0.0.0</version> | |
<description>The framework upon which all Dojo plugins are built.</description> | |
<url>http://n1nja.com/projects/dojo-server/core/</url> | |
<authors> | |
<author> | |
<name>Colin Haber</name> | |
<nickname>N1nja</nickname> | |
<mcname>anorexicpuppy</mcname> | |
<url>http://n1nja.com/</url> | |
</author> | |
</authors> | |
</plugin> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def getTitle(self, filename="manifest.xml"): | |
doc = minidom.parse(filename) | |
plugin = doc.childNodes[0] | |
for node in plugin.childNodes: | |
if node.nodeType == Node.ELEMENT_NODE and node.nodeName == "name": | |
for child in node.childNodes: | |
if child.nodeType == Node.TEXT_NODE: | |
return child.data |
As far as I know yes it is.
unless there is a getElementsByTagName(String) function like in javascript
There is. You can use that to get all the name tags. Although it probably does exactly what you do.
The thing is, that acts recursively, which means that I can't change the spec later to include a <name>
tag nested somewhere else. My implementation only takes data from a first-level <name>
tag.
This is true. Then what you have is best as far as I know.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
getTitle()
is just returning the data inside the<name>
tags. It seems a bit wordy, still, for Python. I've done DOM stuff in Java so I know how verbose it can be, but is there a better way?