This project is the reverse of the first project, the first project is to form an HTML file, the project is to parse the XML file. When parsing the XML file, we used the tool sax, to understand the project's specific process, or to see how sax parsed the XML file.
http://www.ibm.com/developerworks/cn/xml/x-cert/part8/
It would be helpful to understand the whole project by looking at how the above-connected sax works, with some understanding of the parser.
Because to parse an XML file, you need to use the Prase function, and in the course of calling this function there must be some processing tools, that is, the function, ContentHandler this class basically defines all the processing functions, of course, if you have different requirements can be inherited in your own class, Then rewrite the function in the same name, which overrides the function in the parent class.
Websiteconstructor This class is primarily a processing function for various tags. Dispatcher this class is similar to the parent of the handler for the first project, and it is called to find a function with the correct prefix and name. It is worth noting that the elements of the tuple must be comma-added.
Another thing to be aware of is that this project has two kinds of * numbers in use.
The first is in method (*ARGV), where it is obvious that a redundant parameter is used to pass all parameters to the corresponding function at once.
The second is in Os.path.join (*directory), where the role of the * is to expand all the elements in the directory, and divide it into one, part of the path, and then join to join the whole path.
This parsing XML project is to make some different actions for different tags in the XML file, if it is a directory tag, create a folder, if it is a page, then open the corresponding file, write the content to the file. Such a website will be built.
#coding =utf-8 from Xml.sax.handler import ContentHandler from xml.sax Import parse import OS class Dispatcher:u "" is a Mixed classes, similar to the first project, because you want to handle different tags, it is more convenient to specify the processing function by prefix name. Using mixed classes to manage some details is also easy "" "Def Dispatch (Self,prefix,name,attrs=none): U" "in the invocation of various processing methods, first to determine whether the method has or not, if so, then directly call the corresponding function, if not There is then the attribute and name are added to a tuple inside to invoke a default function, that is, ignore other tags, do not do special processing, directly to the output can be "" "Mname = prefix + name.capitalize () dname = ' Default ' + prefix.capitalize () method = GetAttr (self, mname, None) if callable (method): args = () Else:metho D = getattr (self, dname, None) args = name, if prefix = = ' start ': args + = Attrs, #这里需要注意, plus comma if callable (method): Method (*args) def startelement (self,name,attrs): Self.dispatch (' Start ', name, attrs) def endelement (self,name): Self . Dispatch (' End ', name) class Websiteconstructor (Dispatcher, ContentHandler): U "" uses the parser function when using Sax to parse an XML file, This function uses some time handlers to read the files and to process the files accordingly. Inheriting Cotenthandler is because this class implements almost all the time handlers, and of course we can rewrite the time processing function in this program tooverriding uses the Passthrough = False #设定布尔变量, when handling special labels, you need to determine whether the file parser will stop processing def __init__ (self, direc) because you want to ignore some of its internal labels and some special processing. Tory): Self.directory = [directory] self.ensuredirectory () def ensuredirectory (self): U "" "This function is mainly to use the OS module in the operation of the special , create a corresponding directory for each page in the XML file to store the HTML file "" "Path = Os.path.join (*self.directory) if not Os.path.isdir (path): Os.makedirs (Pat h) #如果没有这个路径那么就创建 def characters (self, chars): If Self.passthrough:self.out.write (chars) def defaultstart (SE LF, name, attrs): U "" "" "two default functions are to deal with a number of other tags in a page label, all of these pages are ignored in the processing of" "" If Self.passthrough:self.out.write (' < ' + name ' for key, Val in Attrs.items (): Self.out.write ('%s = '%s '% (key, Val)) Self.out.write (' > ') d EF defaultend (self, name): If Self.passthrough:self.out.write (' </%s> '% name) def startdirectory (self, attrs) : U "" "These two functions are to encounter different directories in the XML file to create, but to delete after the creation, to avoid different directories of the page finally appear under the same directory" "" Self.directory.append (attrs[' name ') self . Ensuredirectory () defEnddirectory (self): Self.directory.pop () def startpage (self,attrs): U "" "two functions to process the page, create the HTML file for this page, and then add the title, open this file, write After the file is closed "" "" "filename = os.path.join (*self.directory+[attrs[' name '] + '. html ']) self.out = open (filename, ' A + ') sel F.writeheader (attrs[' title ']) Self.passthrough = True print U "writing%s"% filename def endpage (self): Self.passthrou GH = False self.writefooter () self.out.close () def writeheader (self, title): Self.out.write ("