MCPcopy Create free account
hub / github.com/ActiveState/code / XmlNode

Class XmlNode

recipes/Python/577451_Easy_XML/recipe-577451.py:3–31  ·  view source on GitHub ↗

An XML node represents a single field in an XML document.

Source from the content-addressed store, hash-verified

1from xml.dom import minidom
2
3class XmlNode:
4 """An XML node represents a single field in an XML document."""
5
6 def __init__(self, domElement):
7 """Construct an XML node from a DOM element."""
8 self.elem = domElement
9
10 @classmethod
11 def makeRoot(cls, xmlFileName):
12 return cls(minidom.parse(xmlFileName))
13
14 def getData(self):
15 """Extract data from a DOM node."""
16 for child in self.elem.childNodes:
17 if child.nodeType == child.TEXT_NODE:
18 return str(child.data)
19 return None
20
21 def getAttributeValue(self, name):
22 """Returns the value of the attribute having the specified name."""
23 return str(self.elem.attributes[name].value)
24
25 def getChild(self, tag):
26 """Returns the first child node having the specified tag."""
27 return XmlNode(self.elem.getElementsByTagName(tag)[0])
28
29 def getChildren(self, tag):
30 """Returns a list of child nodes having the specified tag."""
31 return [XmlNode(x) for x in self.elem.getElementsByTagName(tag)]

Callers 2

getChildMethod · 0.70
getChildrenMethod · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected