[mapguide-internals] Summer of Code Projects

Paul Spencer pspencer at dmsolutions.ca
Wed Feb 28 07:57:32 EST 2007


Bob,

JSON can easily represent any XML, there is a strict formula for  
conversion.  I found a couple of XSLs that claim to convert but I  
couldn't get them to work.  I ended up writing a simple PHP function  
that does the job (embedded below) that produces valid JSON -  
although it has a bit of funkiness which would be eliminated by using  
a C++ library in the mapagent I think.  This works for converting (my  
flavour of) WebLayouts, I havent' tried it with anything else so  
there may be some glitches.  Using this method over directly working  
with the XML in an XmlHttpRequest object resulted in (qualitative  
assessment) substantial improvements in execution speed and load on  
the client browser.

The resulting output would need to be documented I think, at least as  
much as the XML is ;)

Cheers

Paul

function xml2json($domElement) {
     $result = '';
     if ($domElement->nodeType == XML_COMMENT_NODE) {
         return '';
     }
     if ($domElement->nodeType == XML_TEXT_NODE) {
         /* text node, just return content */
         $text = trim($domElement->textContent);
         if ($text != '') {
             $result = '"'.$text.'"';
         } else {
             $text = '""';
         }
     } else {
         /* some other kind of node, needs to be processed */

         $aChildren = array();
         $aValues = array();

         /* attributes are considered child nodes with a special key  
name
            starting with @ */
         if ($domElement->hasAttributes()) {
             foreach($domElement->attributes as $key => $attr) {
                 $len = array_push($aValues, array('"'.$attr- 
 >value.'"'));
                 $aChildren['@'.$key] = $len-1;
             }
         }

         if ($domElement->hasChildNodes()) {
             //has children
             foreach($domElement->childNodes as $child) {
                 if ($child->nodeType == XML_COMMENT_NODE) {
                     continue;
                 }
                 if ($child->nodeType == XML_TEXT_NODE) {
                     $text = trim($child->textContent);
                     if ($text == '') {
                         continue;
                     }
                     array_push($aValues, array('"'.$text.'"'));
                 } else {
                     $childTag = $child->tagName;
                     $json = xml2json($child);
                     if ($json == '') {
                         continue;
                     }
                     if (array_key_exists($childTag, $aChildren)) {
                         array_push($aValues[$aChildren[$childTag]],  
$json);
                     } else {
                         $len = array_push($aValues, array($json));
                         $aChildren[$childTag] = $len - 1;
                     }
                 }
             }
         }

         $nChildren = count($aChildren);
         $nValues = count($aValues);

         if ($nChildren == 0 && $nValues == 0) {
             return '';
         }

         if ($nValues == 1 && $nChildren == 0) {
             $result .= $aValues[0][0];
         } else {
             $bIsObject = true;
             if ($nChildren != $nValues) {
                 $bIsObject = false;
             }
             $result .= $bIsObject ? '{' : '[';

             $sep = '';
             $aChildren = array_flip($aChildren);
             for ($i=0; $i<$nValues; $i++) {
                 $aValue = $aValues[$i];
                 $result .= $sep;

                 if (isset($aChildren[$i])) {
                     if (!$bIsObject) {
                         $result .= '{';
                     }
                     $result .= '"'.$aChildren[$i].'":';
                 }
                 //if (count($aValue) > 1) {
                     $result .= '[';
                     $result .= implode(',', $aValue);
                     $result .= ']';
                 //} else {
                 //    $result .= $aValue[0];
                 //}
                 if (isset($aChildren[$i]) && !$bIsObject) {
                     $result .= '}';
                 }
                 $sep = ',';
             }
             $result .= $bIsObject ? '}' : ']';
         }

     }
     return $result;
}
?>

On 28-Feb-07, at 12:55 AM, Robert Bray wrote:

> The real question is for what methods? Some of the XML output is  
> hierarchically deep. I think it could all be represented by JSON,  
> but would we really want to do all of it? Would we also have to  
> document the corresponding object structures?
>
> Note I am not objecting to the idea, just wondering how far we take  
> it. We need to define the scope in order to contemplate the effort.
>
> Bob
>
> Jason Birch wrote:
>> Hmm.
>>  Anyone have a ballpark idea of what it would take to fit another  
>> content-type handler into the methods that output XML?  Is this  
>> something that would be within the capabilities of a student?
>>  I agree that being able to pull JSON directly from the mapagent  
>> would be a huge boon.
>>  ... and I like saying JSON out loud; I've got a huge ego :)
>>  Jason
>> ________________________________
>> From: mapguide-internals-bounces at lists.osgeo.org on behalf of Paul  
>> Spencer
>> Sent: Mon 2007-02-26 3:46 PM
>> To: MapGuide Internals Mail List
>> Subject: Re: [mapguide-internals] Summer of Code Projects
>> JSON output as an option in mapagent would be most excellent for web-
>> based clients.
>> Paul
>> On 26-Feb-07, at 5:24 PM, Jason Birch wrote:
>>> It looks like Frank has just volunteered to be the OSGeo  
>>> coordinator, and has set up a Wiki page for specific project  
>>> proposals:
>>>
>>> http://wiki.osgeo.org/index.php/Google_Summer_of_Code
>>>
>>> Should we formalize some of the proposals?
>>>
>>> I've added one; I'd welcome feedback... I've got to stuff JSON in  
>>> there somewhere :)
>>>
>>> http://trac.osgeo.org/mapguide/wiki/GoogleSoC2007
>>>
>>> Jason
>>>
>>> ________________________________
>>>
>>> From: mapguide-internals-bounces at lists.osgeo.org on behalf of  
>>> Robert Bray
>>> Sent: Thu 2007-02-22 5:57 PM
>>> To: MapGuide Internals Mail List
>>> Subject: [mapguide-internals] Summer of Code Projects
>>>
>>>
>>>
>>> I have added a page to list potential projects for the Google  
>>> Summer of
>>> Code effort. You can find it here:
>>>
>>> http://trac.osgeo.org/mapguide/wiki/GoogleSoC2007
>>>
>>> Feel free to add your favorite pet projects, elaborate, etc. Keep in
>>> mind these projects are aimed for a single student to complete in a
>>> period of three months.
>>>
>>> Bob
>>> _______________________________________________
>>> mapguide-internals mailing list
>>> mapguide-internals at lists.osgeo.org
>>> http://lists.osgeo.org/mailman/listinfo/mapguide-internals
>>>
>>>
>>> _______________________________________________
>>> mapguide-internals mailing list
>>> mapguide-internals at lists.osgeo.org
>>> http://lists.osgeo.org/mailman/listinfo/mapguide-internals
>> +-----------------------------------------------------------------+
>> |Paul Spencer                          pspencer at dmsolutions.ca    |
>> +-----------------------------------------------------------------+
>> |Chief Technology Officer                                         |
>> |DM Solutions Group Inc                http://www.dmsolutions.ca/ |
>> +-----------------------------------------------------------------+
>> _______________________________________________
>> mapguide-internals mailing list
>> mapguide-internals at lists.osgeo.org
>> http://lists.osgeo.org/mailman/listinfo/mapguide-internals
>> --------------------------------------------------------------------- 
>> ---
>> _______________________________________________
>> mapguide-internals mailing list
>> mapguide-internals at lists.osgeo.org
>> http://lists.osgeo.org/mailman/listinfo/mapguide-internals
> _______________________________________________
> mapguide-internals mailing list
> mapguide-internals at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/mapguide-internals

+-----------------------------------------------------------------+
|Paul Spencer                          pspencer at dmsolutions.ca    |
+-----------------------------------------------------------------+
|Chief Technology Officer                                         |
|DM Solutions Group Inc                http://www.dmsolutions.ca/ |
+-----------------------------------------------------------------+






More information about the mapguide-internals mailing list