Changes between Version 1 and Version 2 of molesparser


Ignore:
Timestamp:
31/10/06 08:40:52 (13 years ago)
Author:
domlowe
Comment:

finished editing Moles parser document

Legend:

Unmodified
Added
Removed
Modified
  • molesparser

    v1 v2  
    3333{{{ 
    3434#!python 
    35 #create metadata description: 
    36 mdID=M.metadataDescriptionID(schemeIdentifier='1',repositoryIdentifier='2', localIdentifier='3') 
    37 dgMD=M.dgMetadataDescription(metadataDescriptionID=mdID) 
    38 #create metadata record 
    39 dgMR=M.dgMetadataRecord(dgMetadataID=dgMID, dgDataEntity=dgDE, dgMetadataDescription=dgMD) 
     35mdID=M.dgMetadataID(schemeIdentifier='1',repositoryIdentifier='2', localIdentifier='3') 
     36dgMR=M.dgMetadataRecord(dgMetadataID=dgMID) 
    4037dgMeta=MRW.dgMetadata(dgMetadataRecord=dgMR) 
    4138}}} 
    4239 
     40So in the above there are 3 classes used: M.dgMetadataID, M.dgMetadataRecord and MRW.dgMetadata (the 'root' element). The resulting XML looks like this: 
     41{{{ 
     42<dgMetadata> 
     43   <dgMetadataRecord> 
     44      <dgMetadataID> 
     45        <schemeIdentifier>1</schemeIdentifier> 
     46        <repostoryIdentifier>2</repositoryIdentifier> 
     47        <localIdentifier>3</localIdentifier> 
     48      </dgMetadataID> 
     49   </dgMetadataRecord> 
     50</dgMetadata> 
    4351 
    44 Rather than give lots of examples this is an explanation of how the parser works. 
    45 MOLES classes exist for elements that have child elements. If the element does not have children, it will not be a class, but will be an attribute of it's parent. So, in the following: 
     52}}} 
     53 
     54So you can see that the hierarchical relationship expressed in python is represented in the XML. 
     55You don't neeed to remember all the class names, you can work them out by remembering that MOLES classes exist for elements that have child elements. If the element does not have children, it will not be a class, but will be an attribute of it's parent. So, in the following: 
    4656{{{ 
    4757         <dgDataGranule> 
     
    6070So the above snippet would be declared like: 
    6171{{{ 
    62 !#python 
    63 dModel=dataModel(repositoryIdentifier='badc.nerc.ac.uk',schemeIdentifier='NDG-A0', localIdentifier='example.xml') 
     72#!python 
     73dModel=M.dataModel(repositoryIdentifier='badc.nerc.ac.uk',schemeIdentifier='NDG-A0', localIdentifier='example.xml') 
    6474DG  = M.dgDataGranule(dataModelID=dModel) 
    6575}}} 
    6676 
    67 There are three things you can pass as a keyword argument. These are: 
     77There are three things you can pass as a keyword argument that affect how each MOLES element is processed. These are: 
    6878 * Another MOLES element 
    6979 * A string 
     
    7484If the value of the keyword argument is a list, then this list can contain multiple strings or multiple MOLES elements. 
    7585 
     86Using lists is necessary when you want a MOLES element to have multiple child elements with the same name. E.g. You may have a dgDataEntity with multiple dgDataGranules. 
     87 
     88In this case you would do (in pseudo-python): 
     89{{{ 
     90#!python 
     91DG1 = M.dgDataGranule(att1 = 'A', att2='B') 
     92DG2 = M.dgDataGranule(att1 = 'C', att2='D') 
     93DG3 = M.dgDataGranule(att1 = 'E', att2='F') 
     94dglist=[DG1, DG2, DG3) 
     95DE = M.dgDataEntity(dgDataGranule=dglist) 
     96}}} 
     97 
     98This will produce: 
     99{{{ 
     100<dgDataEntity> 
     101   <dgDataGranule> 
     102      <att1>A</att1> 
     103      <att2>B</att2> 
     104   </dgDataGranule> 
     105   <dgDataGranule> 
     106      <att1>C</att1> 
     107      <att2>D</att2> 
     108   </dgDataGranule> 
     109   <dgDataGranule> 
     110      <att1>E</att1> 
     111      <att2>F</att2> 
     112   </dgDataGranule> 
     113<dgDataEntity> 
     114}}} 
    76115 
    77116 
    78117 
     118Finally you need to know how to go to and from XML to python. 
     119 
     120There are two methods, toXML() and fromXML(). You only need to call these methods on the root element. 
     121So to convert a MOLES XML document to python do this: 
     122{{{ 
     123#!python 
     124tree=cElementTree.ElementTree(file='moles.xml') 
     125dgMeta=MRW.dgMetadata() 
     126dgMeta.fromXML(tree.getroot()) 
     127}}} 
     128 
     129And to convert to XML: 
     130{{{ 
     131#!python 
     132molestree=dgMeta.toXML() 
     133print cElementTree.dump(molestree) 
     134}}} 
    79135 
    80136 
     137If you are just usign this parser to create MOLES records there may not be much advantage over using cElementTree directly, although it may be more or less readable depending on your preference. However the main advantage comes in being able to manipulate and edit existing MOLES documents. 
    81138 
     139So you could call the fromXML method on the root element of a document and then do things like: 
    82140 
     141{{{ 
     142#!python 
     143for parameterSummary in dgMeta.dgMetadataRecord.dgDataEntity.dgDataSummary.dgParameterSummary: 
     144        #do something to all the parameter summaries 
    83145 
    84  
    85  
    86  
    87  
    88  
    89  
    90  
    91  
    92  
    93  
     146}}}