David Megginson
> Norman Vine writes:
> 
>  > If you are comfortable with Python
>  > 
>  > You can define your objects in Python 
>  > and then dump their XML representation :-)
> 
> A useful XML spec is one that abstracts out the implementation
> differences among various programming languages and applications, so
> that it can be used in ways not originally planned.  For example, this
> is useful and usable by many apps (and human-readable):
> 
>   <airport id="CYOW">
>    <name>MacDonald-Cartier International Airport</name>
>    <icao-airport-code>CYOW</icao-airport-code>
>    <iso-country-code>CA</iso-country-code>
>    <position>
>     <lat-deg>45.318783</lat-deg>
>     <lon-deg>-075.669678</lon-deg>
>     <elev-ft>374</elev-ft>
>    </position>
>   </airport>

All depends on what you are used to I guess
for example I much prefer writing my XML as 
a simple and IMHO more easily read python dictionary

i.e. using your example

<627> tmp
$ cat apt.py
from xmlify import *

airport = {
    'id': "CYOW",
    'name': "MacDonald-Cartier International Airport",
    'icao-airport_code': "CYOW",
    'iso-country-code': "CA",
    'position': { 'lat-deg':  45.318783,
                  'lon-deg': -75.669678,
                  'elev-ft': 374.0 }
        }

xmlify(airport,"airport")    

*** Which when run gives me the desired XML

<628> tmp
$ python apt.py > apt.xml

<629> tmp
$ cat apt.xml
<airport>
  <id>CYOW</id>
  <name>MacDonald-Cartier International Airport</name>
  <position>
    <lat_deg>45.318783</lat_deg>
    <elev_ft>374.0</elev_ft>
    <lon_deg>-75.669678</lon_deg>
  </position>
  <icao_airport_code>CYOW</icao_airport_code>
  <iso_country_code>CA</iso_country_code>
</airport>

*** Here is my little filter

<630> tmp
$ cat xmlify.py
""" simplistic filter to transform a python dictionary into XML """
dict_type={}
indent = ""

def xmlify(obj,name):
    global indent
    print indent+"<%s>"%(name)
    if type(obj) == type(dict_type):
        indent += "  "
        for key in obj.keys():
            data = obj[key]
            if type(data) == type(dict_type):
                xmlify(data,key)
            else:
                data = str(data)
                print indent+"<%s>%s</%s>"%(key,data,key)
    indent = indent[:-2]
    print indent+"</%s>"%(name)



Note as written this will only work with Dictionaries
but is easily extended if desired

Regards

Norman



_______________________________________________
Flightgear-devel mailing list
[EMAIL PROTECTED]
http://mail.flightgear.org/mailman/listinfo/flightgear-devel

Reply via email to