Skip to content

Instantly share code, notes, and snippets.

@colin-haber
Created November 1, 2011 20:55
Show Gist options
  • Save colin-haber/1331874 to your computer and use it in GitHub Desktop.
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?
<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>
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
@colin-haber
Copy link
Author

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?

@flps42
Copy link

flps42 commented Nov 1, 2011

As far as I know yes it is.

unless there is a getElementsByTagName(String) function like in javascript

@flps42
Copy link

flps42 commented Nov 1, 2011

There is. You can use that to get all the name tags. Although it probably does exactly what you do.

@colin-haber
Copy link
Author

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.

@flps42
Copy link

flps42 commented Nov 1, 2011

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