python - How to use namespaces with xml.sax package -
i trying parse xml file, including namespaces, python xml.sax
package, doesn't work. below find example internet, extended namespace "abc". can show me way work it?
this xml file:
<collection shelf="new arrivals"> <movie title="enemy behind"> <type>war, thriller</type> <format>dvd</format> <abc:year>2003</abc:year> <rating>pg</rating> <stars>10</stars> <description>talk us-japan war</description> </movie> <movie title="transformers"> <type>anime, science fiction</type> <format>dvd</format> <abc:year>1989</abc:year> <rating>r</rating> <stars>8</stars> <description>a schientific fiction</description> </movie> <movie title="trigun"> <type>anime, action</type> <format>dvd</format> <episodes>4</episodes> <rating>pg</rating> <stars>10</stars> <description>vash stampede!</description> </movie> <movie title="ishtar"> <type>comedy</type> <format>vhs</format> <rating>pg</rating> <stars>2</stars> <description>viewable boredom</description> </movie> </collection>
this python code. without namespace, works.
from xml.sax import make_parser, handler class handler(handler.contenthandler): def __init__(self): self.currentdata="" self.type = "" self.format = "" self.year = "" self.rating = "" self.stars = "" self.description = "" def startelement(self, tag, attributes): self.currentdata = tag if tag == "movie": print("*****movie*****") title = attributes["title"] print("title:", title) def endelement(self, tag): if self.currentdata == "type": print("type:", self.type) elif self.currentdata == "format": print("format:", self.format) elif self.currentdata == "year": print("year:", self.year) elif self.currentdata == "rating": print("rating:", self.rating) elif self.currentdata == "stars": print("stars:", self.stars) elif self.currentdata == "description": print("description:", self.description) self.currentdata = "" def characters(self, content): if self.currentdata == "type": self.type = content elif self.currentdata == "format": self.format = content elif self.currentdata == "year": self.year = content elif self.currentdata == "rating": self.rating = content elif self.currentdata == "stars": self.stars = content elif self.currentdata == "description": self.description = content parser = make_parser() p = handler() parser.setcontenthandler(p) parser.parse("sax03.xml")
Comments
Post a Comment