PHP » PYTHON |
login |
register |
about
|
PYTHON The XMLReader class
is this article helpful?
|
Python replacement for PHP's The XMLReader class
[
edit
| history
]
Module libxml2
http://dev.laptop.org/~cscott/joyride-api/libxml2-module.html Below is just a sample for those guys who like PHP style. The code is untested code, you must take your own risk. import libxml2 class XMLReader(object): def __init__(self): self.attributeCount = self.depth = 0 self.baseURI = self.localName = self.name = self.namespaceURI = self.nodeType = self.prefix = self.value = self.xmlLang = '' self.hasAttributes = self.hasValue = self.isDefault = self.isEmptyElement = False self.xmlDoc = None def __createFunctions(self): dic_func_map = {'close' : 'Close', 'expand': 'Expand', 'getAttribute' : 'GetAttribute', 'getAttributeNo' : 'GetAttributeNo', 'getAttributeNs' : 'GetAttributeNs', 'getParserProperty' : 'GetParserProp', 'isValid' : 'IsValid', 'lookupNamespace' : 'LookupNamespace', 'moveToAttribute' : 'MoveToAttribute', 'moveToAttributeNo' : 'MoveToAttributeNo', 'moveToAttributeNs' : 'MoveToAttributeNs', 'moveToElement' : 'MoveToElement', 'moveToFirstAttribute' : 'MoveToFirstAttribute', 'moveToNextAttribute' : 'MoveToNextAttribute', 'next' : 'Next', 'readInnerXML' : 'ReadInnerXml', 'readOuterXML' : 'ReadOuterXml', 'readString' : 'ReadString', 'setParserProperty' : 'SetParserProp', 'setRelaxNGSchema' : 'RelaxNGSetSchema', 'setRelaxNGSchemaSource' : 'RelaxNGSetSchema', 'setSchema' : 'SetSchema', } for k, v in dic_func_map.items(): setattr(self, k, getattr(self.xmlDoc, v)) def open(self, str_URI, str_encoding='', int_options=0): try: self.xmlDoc = libxml2.readerForFile(str_URI, str_encoding, int_options) self.__createFunctions() return True except libxml2.treeError, e: print e return False def read(self): if self.xmlDoc: dic_attr_map = {'attributeCount' : 'AttributeCount', 'baseURI' : 'BaseUri', 'depth' : 'Depth', 'localName' : 'LocalName', 'name' : 'Name', 'nodeType' : 'NodeType', 'prefix' : 'Prefix', 'value' : 'Value', 'namespaceURI' : 'NamespaceUri', 'xmlLang' : 'XmlLang', 'hasAttributes' : 'HasAttributes', 'hasValue' : 'HasValue', 'isDefault' : 'IsDefault', 'isEmptyElement' : 'IsEmptyElement', } for k, v in dic_attr_map.items(): setattr(self, k, getattr(self.xmlDoc, v)()) return self.xmlDoc.Read() else: print 'Please use function open() to open a XML file' def xml(self, str_source, str_encoding='', int_options=0): try: self.xmlDoc = libxml2.readerForDoc(str_source, None, str_encoding, int_options) return True except libxml2.treeError, e: print e return False if __name__ == '__main__': f = '[url]http://php.net/manual/en/book.xmlreader.php[/url]' f1 = '[url]http://soap.amazon.com/schemas2/AmazonWebServices.wsdl[/url]' f2 = '[url]http://www.google.com/sitemap.xml[/url]' f3 = '[url]http://news.google.com/?output=rss[/url]' x = XMLReader() #php > $o = new XMLReader(); if x.open(f3): #php > if($o->open($f3)){ while x.read(): #php > while($o->read()){ print x.name #php > echo $o->name; print x.getAttribute('isPermaLink') #php > echo $o->getAttribute('isPermaLink'); print '=========' #php > echo '=========' }} else: #php > else{ print 'Can not open file %s' %f3 # echo 'balah balah'} The XMLReader classIntroductionThe XMLReader extension is an XML Pull parser. The reader acts as a cursor going forward on the document stream and stopping at each node on the way. Class synopsisXMLReader
XMLReader
{
/* Constants */
/* Properties */
/* Methods */
}Properties
Predefined ConstantsXMLReader Node Types
XMLReader Parser Options
Table of Contents
|
more
Recently updated
more
Most requested
more
Last requests
|