You are here:  » xml to php


xml to php

Submitted by toulouse on Fri, 2009-01-30 19:55 in

Hello,

Is it possible to obtain a file php with a file xml?

Here my file xml :

    <list>
        <marker>
            <id>0</id>
            <name>Griffith Observatory</name>
            <addresse>2800 East Observatory Road Los Angeles, CA 90027</addresse>
            <link>http://www.griffithobservatory.org</link>
        </marker>
        <marker>
            <id>1</id>
            <name>California Science Center</name>
            <addresse>700 State Drive Los Angeles, CA 90037</addresse>
            <link>http://www.californiasciencecenter.org</link>
        </marker>
    </list>

...and I would like to obtain a file php :

<?php require_once('...');
$gmap = new GMap();
$markers[0] = new GMarker('2800 East Observatory Road Los Angeles, CA 90027',
                          'Griffith Observatory',
                          'http://www.griffithobservatory.org');
$markers[1] = new GMarker('700 State Drive Los Angeles, CA 90037',
                          'California Science Center',
                          'http://www.californiasciencecenter.org/');
$gmap->showMap($markers);
?>

You think that there is a solution ?

Thanks for your assistance.

Mick

Submitted by support on Fri, 2009-01-30 20:06

Hello Mick,

Absolutely - I think I understand what you are looking to do, and a solution would generally look something like this, where "filename.xml" is a file in the same directory as the script containing XML in the same format as shown in your post. You could of course replace this with a URL or another location....

<?php
  
require_once("MagicParser.php");
  require_once(
"...."); // as required by your script
  
function myRecordHandler($marker)
  {
    global 
$markers;
    
$markers[] = new GMarker($marker["ADDRESSE"],$marker["NAME"],$marker["LINK"]);
  }
  
$markers = array();
  
MagicParser_parse("filename.xml","myRecordHandler","xml|LIST/MARKER/");
  
$gmap = new GMap();
  
$gmap->showMap($markers);
?>

If you have not come across the $marker[] construct in PHP before, this means "append to the array", so the following would be equivalent:

$markers[0] = "Something";
$markers[1] = "Something Else";

and

$markers = array();
$markers[] = "Something";
$markers[] = "Something Else";

Hope this helps!
Cheers,
David.

Submitted by toulouse on Fri, 2009-01-30 21:10

Great ! That functions perfectly !

In fact, that enables me to create one map multiple markers with GoogleMaps. :)

A last question.

I would like to limit the use of the database.
Also, it is possible to use the cache function ?
http://www.magicparser.com/node/136

Thanks for your patience.

Mick

Submitted by support on Fri, 2009-01-30 21:14

Hello Mick,

If you are requesting the XML from a URL, which in turn reads a database then yes, the caching mechanism described in node 136 should work fine, but that would be the only scenario in which it would be appropriate.

It's straight forward to implement - note the part about making sure that your cache/ directory is writable by PHP, that's normally the only part that causes any problem..

Cheers!
David.

Submitted by toulouse on Fri, 2009-01-30 21:48

Hello David,

I confirm ! and your software is very great !

I advise it for two reasons : the quality of your support, and, its universality.
It enables me to pass from Perl via XML towards PHP (Perl => XML => PHP).

Thanks, :)

Mick