Changeset 632


Ignore:
Timestamp:
20/02/06 16:45:46 (13 years ago)
Author:
domlowe
Message:

fixed problem with ElementTree default namespaces

Location:
TI02-CSML/trunk/newparser
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • TI02-CSML/trunk/newparser/Parser.py

    r630 r632  
    5050nsOM = 'http://www.opengis.net/om' 
    5151nsXLINK = 'http://www.w3.org/1999/xlink' 
     52nsXML = 'http://ndg.nerc.ac.uk/csml' 
    5253 
    5354def myQName(uri,tag): 
     
    6263def XLINK(tag): 
    6364    return myQName(nsXLINK,tag) 
     65 
     66def XML(tag): 
     67    return myQName(nsXML.tag) 
    6468 
    6569def checkArrayAttrib(obj,attrib): 
    6670    if not hasattr(obj,attrib) or not isinstance(getattr(obj,attrib),list): 
    6771        setattr(obj,attrib,[]) 
    68  
     72         
     73def fixNameSpaces(csmlFrag): 
     74    #function to convert element tree namespaces from defaults such as 
     75    # ns0 to gml, csml etc. 
     76    for elem in csmlFrag.getiterator(): 
     77        print str(elem.attrib) 
     78        #print 'hello' 
     79    return csmlFrag 
     80         
    6981##class TemplateClass(Base): 
    7082##    def __init__(self,myarg=None,*args,**kwargs): 
     
    90102    def fromXML(self,csmlFrag): 
    91103        if csmlFrag.attrib.has_key(XLINK('href')): 
     104            #print "METADATA PROPERTY FOUND" 
     105            #print (csmlFrag.attrib[XLINK('href')]) 
    92106            self.href = csmlFrag.attrib[XLINK('href')] 
    93107    def toXML(self,csmlFrag): 
     
    639653            frag = FeatureCollection.toXML(self.featureCollection,frag) 
    640654            csmlFrag.append(frag) 
    641         return csmlFrag 
     655            #csmlFrag=fixNameSpaces(csmlFrag) 
     656        return csmlFrag 
  • TI02-CSML/trunk/newparser/test.py

    r630 r632  
    55from Parser import * 
    66from xml.dom.minidom import parseString 
     7import elementtree.ElementTree as etree 
     8import sys 
     9 
     10 
     11#this is a fix to the  ElementTree namespace problem that namespaces are usually represented as ns0, ns1, ns2 etc. 
     12etree._namespace_map.update({ 
     13    'http://www.opengis.net/om': 'om',  'http://www.opengis.net/gml': 'gml','http://ndg.nerc.ac.uk/csml' : 'csml', 'http://www.w3.org/1999/xlink':'xlink'}) 
     14 
     15 
    716 
    817############################################################################## 
     
    1019############################################################################## 
    1120# 1. Test parsing from CSML file 
    12 tree = ElementTree(file='example.xml') 
     21tree = ElementTree(file='examplebanana.xml') 
    1322dataset = Dataset() 
    1423dataset.fromXML(tree.getroot()) 
     
    1625csml = dataset.toXML() 
    1726print parseString(tostring(csml)).toprettyxml() 
     27sys.exit() # stop here. 
     28 
    1829############################################################################## 
    19 # 2. Test construction of objects from scratch 
    20 # 
     30#2. Test construction of objects from scratch 
     31 
    2132# AW changes made to DL code: 
    2233#  (NB: all these derive from the underlying UML model) 
Note: See TracChangeset for help on using the changeset viewer.