import xml.sax.handler import sys # nu wordt de sub-class EmpHandler gedefineerd / sub-class EmpHandler will be defined # You need to subclass xml.sax.ContentHandler and obtain ContentHandler # # The ContentHandler handles tags and attributes class EmpHandler(xml.sax.handler.ContentHandler): def __init__(self): self.inG = False self.inPath = False self.inTspan = False def startElement(self, name, attributes): #print(attributes.getNames()) if name== 'g': attribuutnamen = attributes.getNames() if "transform" in attribuutnamen: print(name, "**************", attributes.getValue('transform')) f.write(name + "-begin-transform " + attributes.getValue('transform') + "\n") else: print(name, "**************") f.write(name + "-begin\n") self.inG = True if name== 'path': print(name, attributes.getValue('d')) f.write(name + " " + attributes.getValue('d') + "\n") self.inPath = True if name == 'tspan': print(name, "x =", attributes.getValue('x'), "y =", attributes.getValue('y')) f.write(name + ", " + "x =" + attributes.getValue('x') + " " + "y =" + attributes.getValue('y') + "\n") self.inTspan = True def characters(self, data): #if self.inPath: #print(data) if self.inTspan: #print("punt", data) #print() #f.write("punt " + data+"\n") #kijken of er spaties zijn / i.e. check whether there are spaces a = data.split() isje="nee" if len(a)==3: #b = a[0]+a[1]+a[2] #c = b.split("=") #print(c, type(c))$ print("punt", a[0], "=", a[2]) print() f.write("punt " + a[0] + " = " + a[2] + "\n") f2.write(a[0] + ", " + a[2] + "\n") isje="ja" #wachten = input("wachten ...") if len(a)==5: #b = a[0]+a[1]+a[2]+a[3]+a[4] #c = b.split("=") #print(c, type(c)) print("punt", a[0], "=", a[2], "=", a[4]) print() f.write("punt " + a[0] + " = " + a[2] + " = " + a[4] + "\n") f2.write(a[0] + ", " + a[2] + ", " + a[4] + "\n") isje="ja" #wachten = input("wachten ...") #kijken of er alleen een =-teken is / look whether there only is an =-sign if isje=="nee": d = data.split("=") if len(d)==1: #dwz GEEN =-teken i.e. no minus sign print("punt", data) print() f.write("punt " + data+"\n") if len(d)==2: print("punt", d[0], "=", d[1]) print() f.write("punt " + d[0] + " = " + d[1] + "\n") f2.write(d[0] + ", " + d[1] + "\n") #wachten = input("wachten ...") if len(d)==3: print("punt", d[0], "=", d[1], "=", d[2]) print() f.write("punt " + d[0] + " = " + d[1] + " = " + d[2] + "\n") f2.write(d[0] + ", " + d[1] + ", " + d[2] + "\n") #wachten = input("wachten ...") def endElement(self, name): if name == 'g': print(name, "--------------") f.write(name + "-einde\n") self.inG =False if name == 'path': self.inPath =False if name == 'tspan': self.inTspan = False import xml.sax f = open("2021-04-15 DETA2.txt", "w") # txt file to write coordinates of crosses and outcrop numbers to f2 = open("2021-04-15 DETA2 alleendubbele.txt", "w") # in case of double values (bugs) (not important) file empty? Good! No bugs. parser = xml.sax.make_parser() #creation of a parser object handler = EmpHandler() parser.setContentHandler(handler) parser.parse('layer ontsluitingsnummers 2021-04-15 DETA.svg') # This is the SVG file with the map to be read i.e. to be parsed f.close() f2.close() #wachten = input("wachten ...")