; docformat = 'rst' ;+ ; Example of using the IDL SAX parser to read a simple XML file, planets.xml ; in the examples/data directory of the IDL distribution. This parser is only ; interested in getting and reporting the planet names. ;- ;+ ; Return the planet names. ; ; :Returns: ; strarr ;- function mgffplanets::getPlanets compile_opt strictarr return, *self.planets end ;+ ; Start tag. ; ; :Params: ; uri : in, required, type=string ; namespace URI associated with element ; local : in, required, type=string ; tag name with any prefix removed ; name : in, required, type=string ; tag name ; attname : in, optional, type=starr ; attribute names, if present ; attvalue : in, optional, type=strarr ; attribute values, if present ;- pro mgffplanets::startElement, uri, local, name, attname, attvalue compile_opt strictarr case strlowcase(name) of 'planet': begin nameInd = where(strlowcase(attname) eq 'name', count) if (count gt 0L) then begin planetName = attvalue[nameInd] *self.planets = n_elements(*self.planets) eq 0L $ ? planetName $ : [*self.planets, planetName] endif end else: ; don't care about this tag endcase end ;+ ; Initialize data structures when starting the document. ;- pro mgffplanets::startDocument compile_opt strictarr if (n_elements(*self.planets) gt 0L) then begin dummy = temporary(*self.planets) endif end ;+ ; Free resources. ;- pro mgffplanets::cleanup compile_opt strictarr self->idlffxmlsax::cleanup ptr_free, self.planets end ;+ ; Create the planets.xml XML SAX parser. ; ; :Returns: ; 1 for success, 0 for failure ; ; :Keywords: ; _extra : in, optional, type=keywords ; keywords to IDLffXMLSAX::init ;- function mgffplanets::init, _extra=e compile_opt strictarr if (~self->idlffxmlsax::init(_extra=e)) then return, 0 self.planets = ptr_new(/allocate_heap) return, 1 end ;+ ; Define instance variables. ; ; :Fields: ; planets ; pointer to strarr of planet names ;- pro mgffplanets__define compile_opt strictarr define = { mgffplanets, inherits IDLffXMLSAX, planets: ptr_new() } end ; Main-level example of using MG_Planets. saxPlanets = obj_new('MGffPlanets') saxPlanets->parseFile, filepath('planets.xml', subdir=['examples', 'data']) print, saxPlanets->getPlanets() obj_destroy, saxPlanets end