PHP Classes

File: functest.php

Recommend this page to a friend!
  Classes of Bas van Gaalen   dom xml class   functest.php   Download  
File: functest.php
Role: Unit test script
Content type: text/plain
Description: Full functionality test and example scripts
Class: dom xml class
An implementation of the DOM XML standard
Author: By
Last change:
Date: 20 years ago
Size: 8,927 bytes
 

Contents

Class file image Download
<?php

   
/***************************************************************************
    This is a functionality test-case for phpdomxml. This file also provides
    several examples on how to use the XML class.
    ***************************************************************************/

    // Turn on all messages, notices and warnings
   
error_reporting(E_ALL);

   
// Set this to the location where all files are stored
   
$base = 'http://www.webtweakers.com:80/phpdomxml/beta/';

   
// Include the xml library
   
include('lib.xml.inc.php');

?>
<html>
<head>
    <title>A small functionality test case</title>
    <style type="text/css">
        body {
            background-color:#fff;
            color:#000;
            font-family:verdana,arial;
            font-size:8pt;
        }
        h1 {
            font-size:14pt;
            margin-bottom:0px;
        }
        .date {
            color:#999;
        }
        .comment {
            font-style:italic;
            display:none;
            width:100%;
            margin-top:5px;
        }
        .rawdoc {
            display:none;
            width:100%;
            height:150px;
            overflow:auto;
            border:1px solid #999;
            margin-top:5px;
        }
        .description {
            font-size:7pt;
            color:#999;
        }
        a {
            color:#009;
            text-decoration:none;
        }
        a:hover {
            text-decoration:underline;
        }
        hr {
            height:1;
            color:#000;
        }
    </style>
    <script type="text/javascript" language="javascript">
        function dspSwitch(id) {
            var d = document.getElementById(id).style;
            d.display = (d.display == 'block')?'none':'block';
        }
    </script>
</head>
<body>

<?php

   
// XML creation test =======================================================

    // Tests:
    // - createElement
    // - createTextNode
    // - attributes
    // - appendChild
    // - toString

    // Tests implicitely:
    // - hasChildNodes
    // - parentNode
    // - nodeType
    // - nodeName
    // - firstChild

    // Create new xml object
   
$test1 = new XML();

   
// Create user-element with id-attribute
   
$user = $test1->createElement('user');
   
$user->attributes['id'] = 1;

   
// Append user-element to xml object
   
$test1->appendChild($user);

   
// Create first-name element and attach it to user-element
   
$fname = $test1->createElement('first-name');
   
$fname->appendChild($test1->createTextNode('Bas'));
   
$user->appendChild($fname);

   
// Create last-name element and attach it to user-element
   
$lname = $test1->createElement('last-name');
   
$lname->appendChild($test1->createTextNode('Gaalen, van'));
   
$user->appendChild($lname);

   
// Create empty (attr only) age element and attach it to user-element
   
$age = $test1->createElement('age');
   
$age->attributes['years'] = 31;
   
$user->appendChild($age);

   
// Show result
   
echo "<h1>XML creation test</h1>";
    echo
"<span class='description'>This example illustrates how to create an XML document using only the methods and properties of the XML class.</span><br><br>";
    echo
htmlentities($test1->toString())."<br><br>";



   
// XML load & parse test ===================================================

    // Tests:
    // - firstChild
    // - nextSibling

    // Tests implicitely:
    // - load
    // - parseXML

    // Create new xml object, and load php's news feed
   
$test2 = new XML('http://www.php.net/news.rss');

    echo
"<hr><h1>XML load & parse test (clickable)</h1>";
    echo
"<span class='description'>The rss-feed of php.net is read and parsed. Javascript is used on the fly to enhance the way the information is displayed.</span><br><br>";
    echo
"<a href='javascript:;' onclick=\"dspSwitch('rawdoc1')\">Raw XML document.</a>";
    echo
"<span id='rawdoc1' class='rawdoc'>".htmlentities($test2->toString())."</span><br><br>";

   
// Go to first item in doc, skipping channel-definition
   
$item = $test2->firstChild->firstChild->nextSibling;

   
// Browse the news items
   
$i = 1;
    while (
$item) {
       
$content = $item->firstChild;
       
$title = $content->firstChild->nodeValue;
       
$link = $content->nextSibling->firstChild->nodeValue;
       
$description = $content->nextSibling->nextSibling->firstChild->nodeValue;
       
$date = $content->nextSibling->nextSibling->nextSibling->firstChild->nodeValue;
        echo
"<a href='javascript:;' onclick=\"dspSwitch('item$i')\">$title</a> [<span class='date'>$date</span>]<br>";
        echo
"<span id='item$i' class='comment'>$description [<a href='$link' target='_blank'>more</a>]<br><br></span>";
       
$item = $item->nextSibling;
       
$i++;
    }



   
// XML messaging test ======================================================

    // Tests:
    // - sendAndLoad

    // Tests implicitely:
    // - toString
    // - parseXML

    // Create new xml object, and load php's news feed
   
$test3 = new XML();
   
$test3->xmlDecl = '<?xml version="1.0" encoding="ISO-8859-1" ?>';

   
// Create query element
   
$query = $test3->createElement('query');

   
// Create name element
   
$name = $test3->createElement('name');
   
$name->appendChild($test3->createTextNode('Bas van Gaalen'));
   
$query->appendChild($name);

   
// Append query to object
   
$test3->appendChild($query);

    echo
"<br><hr><h1>XML messaging test</h1>";
    echo
"<span class='description'>This example sends a message to a server (server.php), which in turn sends back a response.</span><br><br>";
    echo
"Client query:<br><b>".htmlentities($test3->toString())."</b><br><br>";

   
// Create response object
   
$response = new XML();

   
// Send query and load response - change url if needed
   
if ($test3->sendAndLoad($base.'server.php', $response)) {
        echo
"Server response:<br><b>".htmlentities($response->toString())."</b><br>";
    } else {
        echo
"An error occured:<br><b>".$test3->error."</b><br>";
    }



   
// XML cdata parsing test ==================================================

    // Tests:
    // - parseXML
    // - CDATA section

   
echo "<br><hr><h1>XML parsing test</h1>";
    echo
"<span class='description'>This example reads an XML-document, parses it, and converts it back to text. Source and result should be the same.</span><br><br>";

   
$test4 = new XML('test4.xml');

    echo
"<a href='javascript:;' onclick=\"dspSwitch('rawdoc2')\">Raw XML document.</a>";
    echo
"<span id='rawdoc2' class='rawdoc'>".htmlentities($test4->toString())."</span><br><br>";



   
// XML nextSibling & previousSibling test ==================================

    // Explicitely tests:
    // - nextSibling of _xml_get_children-method
    // - previousSibling of _xml_get_children-method

   
echo "<br><hr><h1>XML nextSibling & previousSibling test (1<sup>st</sup> method)</h1>";
    echo
"<span class='description'>An XML-document is read, parsed and the contents are displayed using nextSibling and previousSibling. The sibling-pointers are set within the parsing function.</span><br><br>";

   
$test5 = new XML('test5.xml');

    echo
"<a href='javascript:;' onclick=\"dspSwitch('rawdoc3')\">Raw XML document.</a>";
    echo
"<span id='rawdoc3' class='rawdoc'>".htmlentities($test5->toString())."</span><br><br>";

    echo
"<p>Walk tree with <b>nextSibling</b><br>";
   
$item = $test5->firstChild->firstChild;
    while (
$item) {
        echo
$item->attributes['number'].": ".$item->firstChild->nodeValue."<br>";
       
$item = $item->nextSibling;
    }

    echo
"<p>Walk tree with <b>previousSibling</b><br>";
   
$item = $test5->firstChild->lastChild;
    while (
$item) {
        echo
$item->attributes['number'].": ".$item->firstChild->nodeValue."<br>";
       
$item = $item->previousSibling;
    }



   
// XML nextSibling & previousSibling, 2nd method - test ====================

    // Explicitely tests:
    // - nextSibling of appendChild-method
    // - previousSibling of appendChild-method

    // Also tests:
    // - createElement
    // - createTextNode
    // - attributes

   
echo "<br><hr><h1>XML nextSibling & previousSibling test (2<sup>nd</sup> method)</h1>";
    echo
"<span class='description'>An XML-document is build from a PHP array. The contents are displayed using nextSibling and previousSibling. The sibling-pointers are set within the appendChild-method.</span><br><br>";

   
$list = array(
       
'some text here',
       
'some more text here',
       
'even more text here',
       
'still more text here'
   
);

   
$test6 = new XML();
   
$test6->xmlDecl = '<?xml version="1.0"?>';

   
$docelm = $test6->createElement('document');

   
// Loop through list and create XML elements
   
foreach($list as $key=>$item) {

       
// $elmname = string, $$elmname = object
       
$key++;
       
$elmname = 'item'.$key;
        $
$elmname = $test6->createElement('plain-text');
        $
$elmname->attributes['number'] = $key;

        $
$elmname->appendChild($test6->createTextNode($item));

       
$docelm->appendChild($$elmname);
    }
   
$test6->appendChild($docelm);

    echo
"<a href='javascript:;' onclick=\"dspSwitch('rawdoc4')\">Raw XML document.</a>";
    echo
"<span id='rawdoc4' class='rawdoc'>".htmlentities($test6->toString())."</span><br><br>";

    echo
"<p>Walk tree with <b>nextSibling</b><br>";
   
$item = $test6->firstChild->firstChild;
    while (
$item) {
        echo
$item->attributes['number'].": ".$item->firstChild->nodeValue."<br>";
       
$item = $item->nextSibling;
    }

    echo
"<p>Walk tree with <b>previousSibling</b><br>";
   
$item = $test6->firstChild->lastChild;
    while (
$item) {
        echo
$item->attributes['number'].": ".$item->firstChild->nodeValue."<br>";
       
$item = $item->previousSibling;
    }

?>

</body>
</html>