Changes between Version 7 and Version 8 of CSMLParserHowTo


Ignore:
Timestamp:
04/02/08 10:41:39 (12 years ago)
Author:
domlowe
Comment:

editing parser how to (not finished)

Legend:

Unmodified
Added
Removed
Modified
  • CSMLParserHowTo

    v7 v8  
    1 = Introduction to the CSML Parser & API = 
    2  
    3 ''OUT OF DATE: THIS PAGE REQUIRES UPDATING TO REFLECT CHANGED MODULE STRUCTURE. However it may still be worth reading.'' 
     1= Introduction to the CSML Parser & API, Updated for CSML V2 = 
    42 
    53This document covers: 
     
    3432The online parser  is simply a web interface to the parser, and allows you to parse a CSML document. You can't do anything with the parsed document, but it is useful as a way of verifying what the parser 'sees' when it parses your CSML document. If you don't have a CSML document, you can download one {HERE}. 
    3533The parser is located at: 
    36 http://proj.badc.rl.ac.uk/cgi-bin/csml/parseTest.py 
     34http://proj.badc.rl.ac.uk/cgi-bin/csml2/parseTest.py 
    3735Simply browse to your CSML file and submit your query to see if your file parses.  
    38  
    39 '''''Update for CSML 2'''''  The parser for CSML v2 is here http://proj.badc.rl.ac.uk/cgi-bin/csml2/parseTest.py 
    4036 
    4137 === Parsing in python === 
     
    4440 
    4541The parser is written in Python, so   you will need Python installed to use it.  
    46 When you have python installed, try and run the parser code. Check out the parser code from subversion. The code you need is here: [http://proj.badc.rl.ac.uk/ndg/browser/TI02-CSML/trunk]. You will need the entire csml directory and you will need basictest.py and example.xml from here: [http://proj.badc.rl.ac.uk/ndg/browser/TI02-CSML/trunk/Examples] 
     42When you have python installed, try and run the parser code. Check out the parser code from subversion. The code you need is here: [http://proj.badc.rl.ac.uk/ndg/browser/TI02-CSML/trunk]. You will need the entire csml directory and you will need basictest.py and example.xml from here: [http://proj.badc.rl.ac.uk/ndg/browser/TI02-CSML/trunk/csml/Examples/Parsing] 
    4743 
    4844Try and run the file basictest.py by typing: 
     
    5753{{{ 
    5854#!python 
    59 tree = ElementTree(file='example.xml') 
    60 dataset = csml.parser.Dataset() 
     55import csml 
    6156 
    62 #Calling the fromXML method reads the CSML into memory. 
    63 dataset.fromXML(tree.getroot()) 
     57#A path to a file 
     58f='example.xml' 
    6459 
    65 #This creates a new CSML document string from the CSML objects in memory. 
    66 #Hopefully the CSML output should be the same as the CSML it read in. 
     60#Create empty dataset object and parse the file,f into it. 
     61dataset=csml.parser.Dataset(file=f) 
     62 
     63#the toXML() method returns an elementtree element instance: 
    6764csmldoc = dataset.toXML() 
     65print csmldoc 
    6866 
    69 #print it and see: 
    70 strCSML=parser_extra.PrettyPrint(csmldoc) # this just tidies up the formatting 
    71 strCSML=parser_extra.removeInlineNS(strCSML)  #this just tidies up the namespaces 
     67#And the toPrettyXML() method returns a string, with correct formatting and namespaces. 
     68#Tidy up and print the CSML document: 
     69strCSML=dataset.toPrettyXML() 
    7270print strCSML 
     71 
    7372}}} 
    74  
    75  
    76 So all we are doing is creating a Dataset parser object and saying 'parse all the XML from tree.getroot()'. i.e. parse the entire CSML document. 
    7773 
    7874So now the object called 'dataset' is a representation of the CSML document in memory. You can navigate this document directly by using python attributes e.g.: 
     
    8076{{{ 
    8177#!python 
     78#Get the dataset id: 
     79print dataset.id 
     80 
    8281#Reading the href attribute of the domainReference for a feature and print it: 
    8382print dataset.featureCollection.members[3].profileSeriesDomain.domainReference.times.href