PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Temperini Mirko   Simple Object Element   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: example file
Class: Simple Object Element
Parse XML documents and export as arrays or JSON
Author: By
Last change:
Date: 14 years ago
Size: 4,799 bytes
 

Contents

Class file image Download
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  <title>simpleObjectElement</title>
  <style>
  body{
    background: #000;
    }
 
  pre{
    overflow-x: scroll;
    border: 3px dashed #fff;
    background-color: silver;
    padding: 8px;
    }
  </style>
  </head>
  <body>


<?php
include 'simple_object_element.class.php';


$array=array(
    array(
'name'=>'Luciano','surname'=>'Pavarotti'),
    array(
'name'=>'Will','surname'=>'Smith'),
    array(
'name'=>'Al','surname'=>'Pacino')
    );

$json='{
    0:{"name":"Luciano","surname":"Pavarotti"},
    1:{"name":"Will","surname":"Smith"},
    2:{"name":"Al","surname":"Pacino"}
    }'
;


$xml='
    <star>
        <name>Luciano</name>
        <surname>Pavarotti</surname>
    </star>
    <star>
        <name>Will</name>
        <surname>Smith</surname>
    </star>
    <star>
        <name>Al</name>
        <surname>Pacino</surname>
    </star>
    '
;

/*********************************************************************************************************/

//using an array
echo '<pre>';
echo
'<h2 align="center">Array type</h2>';
$test_1 = new SimpleObjectElement;
$test_1 -> set_integer_tag('star');
$test_1 -> load_object($array);




echo
"<hr>";
echo
'<h3 align="center">Source</h3>';
echo
"Source was a ".$test_1->get_type()." object<br />";

echo
"<hr>";
echo
'<h3 align="center">Xpath result query</h3>';
foreach(
$test_1->xpath('/root/star/surname') as $star){
   
print_r($star);
    }

echo
"<hr>";
echo
'<h3 align="center">the xml exported as array</h3>';
print_r($test_1->export_object('array'));

echo
"<hr>";
echo
'<h3 align="center">the xml exported as json</h3>';
print_r($test_1->export_object('json'));




echo
"<hr>";
echo
'<h3 align="center">the xml exported as is</h3>';

echo
"we have added a new star: Michael Jackson<br /><br />";
$new_star=$test_1 -> addChild('start');
$new_star -> addChild('name','Michael');
$new_star -> addChild('surname','Jackson');

echo
htmlentities($test_1->export_xml());

echo
"</pre>";

/*********************************************************************************************************/

//using a json object
echo '<pre>';
echo
'<h2 align="center">JSON type</h2>';
$test_2 = new SimpleObjectElement;
$test_2 -> set_integer_tag('star');
$test_2 -> load_object($json);

echo
"<hr>";
echo
'<h3 align="center">Source</h3>';
echo
"Source was a ".$test_2->get_type()." object<br />";

echo
"<hr>";
echo
'<h3 align="center">Xpath result query</h3>';
foreach(
$test_2->xpath('/root/star/surname') as $star){
   
print_r($star);
    }

echo
"<hr>";
echo
'<h3 align="center">the xml exported as array</h3>';
print_r($test_2->export_object('array'));

echo
"<hr>";
echo
'<h3 align="center">the xml exported as json</h3>';
print_r($test_2->export_object('json'));

echo
"</pre>";

/*********************************************************************************************************/

//using an xml string
echo '<pre>';
echo
'<h2 align="center">XML type</h2>';
$test_3 = new SimpleObjectElement;
$test_3 -> load_xml($xml,true);

if(
$test_3 -> get_error() )
    echo
$test_3->get_error();

echo
"<hr>";
echo
'<h3 align="center">Source</h3>';
echo
"Source was a ".$test_1->get_type()." object<br />";

echo
"<hr>";
echo
'<h3 align="center">Xpath result query</h3>';
foreach(
$test_3->xpath('/root/star/surname') as $star){
   
print_r($star);
    }

echo
"<hr>";
echo
'<h3 align="center">the xml exported as array</h3>';
print_r($test_3->export_object('array'));

echo
"<hr>";
echo
'<h3 align="center">the xml exported as json</h3>';
print_r($test_3->export_object('json'));

echo
"</pre>";


/*********************************************************************************************************/

//generating an error
echo '<pre>';
echo
'<h2 align="center">error</h2>';
$test_4 = new SimpleObjectElement;

echo
<<<OUT
check error mode 1:<br />
if(!\$test_4->load_object('foo bar')){
    var_dump(\$test_4->get_error());
    }
<br />
OUT;

if(!
$test_4->load_object('foo bar')){
   
var_dump($test_4->get_error());
    echo
"<br />";
    }

echo
<<<OUT
chech mode 2:<br />
if(\$test_4->get_error()){
    var_dump(\$test_4->get_error());
    }
<br />
OUT;

if(
$test_4->get_error()){
   
var_dump($test_4->get_error());
    echo
"<br />";
    }

echo
"Source was a ".$test_4->get_type()." object<br />";

foreach(
$test_4->xpath('/root/star/surname') as $star){
   
print_r($star);
    }
echo
"</pre>";

?>

  </body>
</html>