diff --git a/XML_Processing/DOM_module.py b/XML_Processing/DOM_module.py new file mode 100644 index 0000000..2f78535 --- /dev/null +++ b/XML_Processing/DOM_module.py @@ -0,0 +1,18 @@ +import xml.dom.minidom +import os + + +domtree = xml.dom.minidom.parse("XML_Processing\group.xml") +group = domtree.documentElement + +persons = group.getElementsByTagName("person") + +for person in persons: + print("--- Persons ---") + if person.hasAttribute("id"): + print("ID: %s" % person.getAttribute("id")) + + name = person.getElementsByTagName ("name")[0] + age = person.getElementsByTagName("age")[0] + weight = person.getElementsByTagName("weight")[0] + height = person.getElementsByTagName("height")[0] diff --git a/XML_Processing/SAX_module.py b/XML_Processing/SAX_module.py new file mode 100644 index 0000000..448a688 --- /dev/null +++ b/XML_Processing/SAX_module.py @@ -0,0 +1,38 @@ +import xml.sax + +class GroupHandler(xml.sax.ContentHandler): + + def startElement(self, name, attrs): + self.current = name + if self.current == "person": + print("--- Person ---") + id = attrs["id"] + print("ID: %s" % id) + + def endElement(self, name): + if self.current == "name": + print("Name: %s" % self.name) + elif self.current == "age": + print("Age: %s" % self.age) + elif self.current == "weight": + print("Weight: %s" % self.weight) + elif self.current == "height": + print("Height: %s" % self.height) + self.current = "" + + def characters(self, content): + if self.current == "name": + self.name = content + elif self.current == "age": + self.age = content + elif self.current == "weight": + self.weight = content + elif self.current == "height": + self.height = content + + +handler = GroupHandler() +parser = xml.sax.make_parser() +parser.setContentHandler(handler) +parser.parse("XML_Processing\group.xml") + diff --git a/XML_Processing/group.xml b/XML_Processing/group.xml new file mode 100644 index 0000000..947ee4d --- /dev/null +++ b/XML_Processing/group.xml @@ -0,0 +1,33 @@ + + + + Jonh Smith + 20 + 80 + 188 + + + Mike Davis + 45 + 82 + 185 + + + Anna Johnson + 33 + 67 + 167 + + + Bob Smith + 60 + 70 + 174 + + + Sarah Pitt + 12 + 50 + 152 + + \ No newline at end of file