Changeset 3295 for TI02-CSML


Ignore:
Timestamp:
04/02/08 10:26:11 (11 years ago)
Author:
domlowe
Message:

Added optional file keyword to Dataset init method. Added toPrettyXML() method.

Location:
TI02-CSML/trunk/csml
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • TI02-CSML/trunk/csml/Examples/Parsing/basictest.py

    r3294 r3295  
    77f='example.xml' 
    88 
    9 #Create empty dataset object and parse the file into it. 
    10 #This creates a new CSML document string from the CSML objects in memory. 
    11 dataset=csml.parser.Dataset() 
    12 dataset.parse(f) 
     9#Create empty dataset object and parse the file,f into it. 
     10#This creates a new CSML document string from the CSML objects in memory: 
     11dataset=csml.parser.Dataset(file=f) 
    1312 
     13#or the long way: 
     14#dataset=csml.parser.Dataset() 
     15#dataset.parse(f) 
    1416 
    15 #Output the CSML from the dataset object as a string.  
     17#Now output the CSML from the dataset object. 
    1618#Hopefully the CSML output should be the same as the CSML it read in: 
     19 
     20#the toXML() method returns an elementtree element instance: 
    1721csmldoc = dataset.toXML() 
     22print csmldoc 
    1823 
     24#And the toPrettyXML() method returns a string, with correct formatting and namespaces. 
    1925#Tidy up and print the CSML document: 
    20 strCSML=csml.parser_extra.PrettyPrint(csmldoc) 
    21 strCSML=csml.parser_extra.removeInlineNS(strCSML) 
     26strCSML=dataset.toPrettyXML() 
    2227print strCSML 
    2328 
  • TI02-CSML/trunk/csml/parser.py

    r3097 r3295  
    12461246class Dataset(csElement):     
    12471247    ''' Dataset class, needed as root of tree''' 
    1248     def __init__(self, **kwargs): 
     1248    def __init__(self, file=None, **kwargs): 
    12491249        a=['id'] 
    12501250        addatts(self,a) 
     
    12541254        addchildren(self,children) 
    12551255        csElement.__init__(self,**kwargs) 
     1256        if file is not None: 
     1257            self.parse(file) 
    12561258     
    12571259    def toXML(self): 
     1260        ''' returns XML as elementtree instance''' 
    12581261        csmlfrag=ET.Element(CSML('Dataset')) 
    12591262        csElement.toXML(self, csmlfrag) 
    12601263        return csmlfrag 
    1261     #def _getSubstitutionType(self,tag):   
    1262         #if tag==CSML('NetCDFExtract'): 
    1263             #return 'NetCDFExtract' 
    1264         #elif tag==CSML('NASAAmesExtract'): 
    1265             #return 'NASAAmesExtract' 
    1266         #else: return 'ArrayDescriptor' 
    1267     #def _getReverseSubsType(self, typename): 
    1268         #if typename== 'NetCDFExtract': 
    1269             #return CSML('NetCDFExtract') 
    1270         #elif typename == 'NASAAmesExtract': 
    1271             #return CSML('NASAAmesExtract') 
    1272         #else: return CSML('ArrayDescriptor') 
    1273          
    1274         return typename 
    1275  
     1264     
     1265    def toPrettyXML(self): 
     1266        '''returns nicely formatted XML as string ''' 
     1267        csmldoc = self.toXML() 
     1268        #pretty print the result 
     1269        strCSML=csml.parser_extra.PrettyPrint(csmldoc) 
     1270        #and fix elementtree namespaces 
     1271        strCSML=csml.parser_extra.removeInlineNS(strCSML) 
     1272        return strCSML 
    12761273     
    12771274def main(): 
Note: See TracChangeset for help on using the changeset viewer.