PHP Classes
elePHPant
Icontem

File: xmltest.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of sushil bajimaya  >  xmlReader  >  xmltest.php  >  Download  
File: xmltest.php
Role: Example script
Content type: text/plain
Description: tests the xmlReader class
Class: xmlReader
Parse an XML document and build a nested array
Author: By
Last change:
Date: 8 years ago
Size: 3,603 bytes
 

Contents

Class file image Download
<?php
   
require_once("lib/domIT/xml_domIT_include.php");
   
    class
xmlReader
   
{
        var
$domDocument;
        var
$childNodes;
        var
$strPath;
       
        function
xmlReader($strDir, $strXMLFile)
        {
           
$this->__construct($strDir, $strXMLFile);
        }
       
        function
__construct($strDir, $strXMLFile)
        {
           
$this->strPath = $strDir."/".$strXMLFile;
           
$this->domDocument = & new DOMIT_Document();
        }
       
        function
loadFile()
        {
            return
$this->domDocument->loadXML($this->strPath);
        }
       
        function
retRootElement()
        {
            return
$this->domDocument->documentElement;
        }
       
        function
parseNodes($rootNode, & $dest)
        {
            global
$count;
           
$nodeName =& $rootNode->nodeName;
           
//$dest[$nodeName] = array();
           
if($rootNode->hasAttributes())
            {
               
$attributes = & $rootNode->attributes;
                for(
$i = 0; $i < $attributes->getLength(); $i++)
                {
                   
$attrib = & $attributes->item($i);
                   
$dest[$nodeName][$attrib->getName()] = $attrib->getValue();
                }
            }
            if(
$rootNode->hasChildNodes())
            {
               
$cNodes = & $rootNode->childNodes;
                foreach(
$cNodes as $node)
                {
                    if(
$node->hasChildNodes())
                    {
                       
//foreach($cNodes as $node)
                       
$this->parseNodes($node, $dest[$nodeName][]);
                    }
                    else
//if($node->nodeType == 1)
                   
{
                       
$endNode = & $rootNode->firstChild;
                       
$dest[$nodeName] = $endNode->nodeValue;
                    }
                }
            }
           
/*if($rootNode->nodeType == 1)
            {
                $endNode = & $rootNode->firstChild;
                $dest[$nodeName] = $endNode->nodeValue;
            }*/

           
return $dest;
        }
    }
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>

<body>
<?php
    $strDir
= "xml";
   
$strFile = "tSubmenu2.xml";
   
   
$domDoc = new xmlReader($strDir, $strFile);
   
$status = $domDoc->loadFile();
   
    if(
$status)
    {
       
$arData = array();
       
$rootElement =& $domDoc->retRootElement();
       
       
$dest = array();
       
$dest = $domDoc->parseNodes($rootElement, $dest);
        echo
"<pre>";
       
var_dump($dest);
        echo
"</pre>";
    }
   
   
//----------------------------------------------------------------------
/*
    echo "<br><br><br>------------------------------------------<br>";
    $domDocument =& new DOMIT_Document();
    $success = $domDocument->loadXML("xml/tSubmenu2.xml");
    if($success)
    {
        $docElement = & $domDocument->documentElement;
        if($docElement->hasChildNodes())
        {
            $childNode = & $docElement->childNodes;
            foreach($childNode as $cNode)
            {
                echo $cNode->nodeName;
                if($cNode->hasAttributes())
                {
                    echo "<blockquote>";
                    echo "Attribute(s):-<br>";
                    $attributes =& $cNode->attributes;
                    echo "<blockquote><blockquote>";
                    for($i = 0; $i < $attributes->getLength(); $i++)
                    {
                        $attrib = & $attributes->item($i);
                        echo $attrib->getName()."=>".$attrib->getValue()."<br>";
                    }
                    echo "</blockquote></blockquote>";
                    echo "</blockquote>";
                }
                if($cNode->hasChildNodes())
                {
                    echo "<blockquote>";
                    $count = & $cNode->childCount;
                    echo "child nodes: $count<br>";
                    echo "Items:-<br>";
                    echo "<blockquote><blockquote>";
                    $nodes =& $cNode->childNodes;
                    foreach($nodes as $endNode)
                    {
                        $textNode =& $endNode->firstChild;
                        echo $endNode->nodeName."=>".$textNode->nodeValue."<br>";
                    }
                    echo "</blockquote></blockquote>";
                    echo "</blockquote>";
                }
            }
        }
    }*/
?>
</body>
</html>