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

Class Element

recipes/Python/149368_xml2obj/recipe-149368.py:8–44  ·  view source on GitHub ↗

A parsed XML element

Source from the content-addressed store, hash-verified

6from xml.parsers import expat
7
8class Element:
9 'A parsed XML element'
10 def __init__(self,name,attributes):
11 'Element constructor'
12 # The element's tag name
13 self.name = name
14 # The element's attribute dictionary
15 self.attributes = attributes
16 # The element's cdata
17 self.cdata = ''
18 # The element's child element list (sequence)
19 self.children = []
20
21 def AddChild(self,element):
22 'Add a reference to a child element'
23 self.children.append(element)
24
25 def getAttribute(self,key):
26 'Get an attribute value'
27 return self.attributes.get(key)
28
29 def getData(self):
30 'Get the cdata'
31 return self.cdata
32
33 def getElements(self,name=''):
34 'Get a list of child elements'
35 #If no tag name is specified, return the all children
36 if not name:
37 return self.children
38 else:
39 # else return only those children with a matching tag name
40 elements = []
41 for element in self.children:
42 if element.name == name:
43 elements.append(element)
44 return elements
45
46class Xml2Obj:
47 'XML to Object'

Callers 1

StartElementMethod · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected