Free Software Server Environment : PhpXml

HomePage :: Categories :: PageIndex :: RecentChanges


Search this site: FSSE.INFO is produced by "osde8info" :

Revision [731]

The oldest known version of this page was edited on 2008-09-01 15:27:47 by FsseInfo
http://uk.php.net/manual/en/refs.xml.php

PHP XML

There are a number of different ways to you can use PHP to manipulate XML.

PHP & SimpleXML
Here is an example of a XML navigator / simplifier written in SimpleXML !
function nav($x,$level=1) {
    foreach ($x->children() as $ng) {
        echo str_repeat(' ',$level), $ng->getName();
        $haschildren=count($ng->children());
        if (!$haschildren) {
            echo '=', $ng;
        }
        foreach ($ng->attributes() as $a => $v) {
          echo ' ' , $a, '=', $v ;
        }
        echo PHP_EOL ;
        if ($haschildren) {
            nav($ng,$level+1);
        }
    }
}

$file = $argv[1];

if (file_exists($file)) {
    $xml = simplexml_load_file($file);
    nav($xml);
}


input
<?xml version="1.0" encoding="UTF-8"?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.1" creator="Nokia Sports Tracker" xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance"
xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">

<metadata>
<name>2008/09/01 8:49 am</name>
<desc>Cycling</desc>
<author><name>Joe Average</name></author>
<time>2008-09-01T08:49:40.72</time>
</metadata>
<trk>
<name>2008/09/01 8:49 am</name>
<trkseg>
<trkpt lat="51.464503" lon="-0.169937">
<ele>32.5</ele>
<speed>0.3</speed>
<course>318.2</course>
<desc>Speed 0.3 mph Distance 0.00 mi</desc>
<time>2008-09-01T08:49:43.31</time>
<name>1</name>
</trkpt>


output
 metadata
  name=2008/09/01 8:49 am
  desc=Cycling
  author
   name=Joe Average
  time=2008-09-01T08:49:40.72
 trk
  name=2008/09/01 8:49 am
  trkseg
   trkpt lat=51.464503 lon=-0.169937
	ele=32.5
	speed=0.3
	course=318.2
	desc=Speed 0.3 mph Distance 0.00 mi
	time=2008-09-01T08:49:43.31
	name=1



REFERRERS
PHP
Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by WikkaWiki
Page was generated in 0.0777 seconds