<?php
//
// Test: Run the script bellow and read the output
//
require_once ("arr2xml.class.php");
$txt_xml = '<?xml version="1.0"?>'
. '<collection owner="Mozart" created="01/12/2007">'
. '<cd type="bolacha">'
. '<title>Fight for your mind</title>'
. '<artist>Ben Harper</artist>'
. '<year>1995</year>'
. '</cd>'
. '<cd>'
. '<title tipo="NP3" origem="Inglaterra">Electric Ladyland</title>'
. '<artist>Jimi Hendrix</artist>'
. '<year>1997</year>'
. '</cd>'
. '</collection>'
;
$objXML = new DOMDocument(); // Create the XML document
$objXML->loadXML(utf8_encode($txt_xml)); // Charge the XML content from $txt_xml
echo "TEST 1: Starting from an XML object<BR>";
echo "<BR>===== Incoming XML Object:<BR><BR>"; // Separator
print htmlentities($objXML->saveXML()); // Print the incoming XML object
$transf = new xml_to_from_array(); // Instanciate the class
$_SESSION = $transf->xml_to_array($objXML); // Convert the XML object to array named '$_SESSION'
echo "<BR><BR>===== Generated array:<BR><BR>"; // Separator
show_array ($_SESSION); // Print the outcoming array
$xml = $transf->array_to_xml($_SESSION); // Convert the array back to XML
echo "<BR>===== Recovered XML Object (must be equal to incoming XML Object)<BR><BR>"; // Separator
print htmlentities($xml->saveXML()); // Print the recovered XML object
echo "<BR><BR>===== END OF TEST 1<BR>"; // Separator
$_SESSION = Array();
$_SESSION['collection']['atributos']['owner'] = "Mozart";
$_SESSION['collection']['atributos']['created'] = "01/12/2007";
$_SESSION['collection']['cd']['1']['atributos']['type'] = "bolacha";
$_SESSION['collection']['cd']['1']['title'] = "Fight for your mind";
$_SESSION['collection']['cd']['1']['artist'] = "Ben Harper";
$_SESSION['collection']['cd']['1']['year'] = "1995";
$_SESSION['collection']['cd']['2']['title']['atributos']['tipo'] = "NP3";
$_SESSION['collection']['cd']['2']['title']['atributos']['origem'] = "Inglaterra";
$_SESSION['collection']['cd']['2']['title']['valor'] = "Electric Ladyland";
$_SESSION['collection']['cd']['2']['artist'] = "Jimi Hendrix";
$_SESSION['collection']['cd']['2']['year'] = "1997";
echo "<BR>TEST 2: Starting from an array with a root node<BR>";
echo "<BR>===== Incoming array:<BR><BR>"; // Separator
show_array ($_SESSION); // Print the incoming array
$transf = new xml_to_from_array(); // Instanciate the class
$xml = $transf->array_to_xml($_SESSION); // Convert the array to an XML object
echo "<BR>===== Generated XML:<BR><BR>"; // Separator
print htmlentities($xml->saveXML()); // Print the outcoming XML object
$_S = $transf->xml_to_array($xml); // Convert the XML object back to array
echo "<BR><BR>===== Recovered array (must be equal to incoming array)<BR><BR>"; // Separator
show_array($_S); // Print the recovered array
echo "<BR>===== END OF TEST 2<BR>"; // Separator
$_SESSION = Array();
$_SESSION['cd']['1']['atributos']['type'] = "bolacha";
$_SESSION['cd']['1']['title'] = "Fight for your mind";
$_SESSION['cd']['1']['artist'] = "Ben Harper";
$_SESSION['cd']['1']['year'] = "1995";
$_SESSION['cd']['2']['title']['atributos']['tipo'] = "NP3";
$_SESSION['cd']['2']['title']['atributos']['origem'] = "Inglaterra";
$_SESSION['cd']['2']['title']['valor'] = "Electric Ladyland";
$_SESSION['cd']['2']['artist'] = "Jimi Hendrix";
$_SESSION['cd']['2']['year'] = "1997";
$_SESSION['tape']['atributos']['type'] = "cassete";
$_SESSION['tape']['title'] = "Soul Train";
$_SESSION['tape']['artist'] = "Mamas and Papas";
$_SESSION['tape']['year'] = "1968";
echo "<BR>TEST 3: Starting from array whose first dimension is a vector<BR>";
echo "<BR>===== Incoming array:<BR><BR>"; // Separator
show_array ($_SESSION); // Print the incoming array
$transf = new xml_to_from_array(); // Instanciate the class
$xml = $transf->array_to_xml($_SESSION); // Convert the array to an XML array
echo "<BR>===== Generated XML Object:<BR>MBR>"; // Separator
print htmlentities($xml->saveXML()); // Print the outcoming XML object
$_S = $transf->xml_to_array($xml); // Convert the XML object back to array
echo "<BR><BR>===== Recovered array (must be equal to incoming array)<BR><BR>"; // Separator
show_array($_S); // Print the recovered array
echo "<BR>===== END OF TEST 3<BR>"; // Separator
die;
//
// Function to print arrays in readable form
//
function show_array($array_in, $indentador=""){
if (is_array($array_in)){
while (list ($key, $val) = each ($array_in)) { // lista o array
echo $indentador . "$key => $val<br>";
if (is_array($val)) {
$indent = "(...)" . $indentador;
show_array($val, $indent);
}
}
}
}
// Fim do teste
?>
|