PHP Classes

File: lib/Helper/ExtDOMDocument.php

Recommend this page to a friend!
  Classes of Stefan Kientzler  >  PHPInfo Compare Online  >  lib/Helper/ExtDOMDocument.php  >  Download  
File: lib/Helper/ExtDOMDocument.php
Role: Auxiliary script
Content type: text/plain
Description: Helper class for easier XML access
Class: PHPInfo Compare Online
Parse and compare PHP configuration versions
Author: By
Last change:
Date: 1 year ago
Size: 2,606 bytes
 

Contents

Class file image Download
<?php
namespace lib\Helper;

/**
 * extends DOMDocumenta with few little helpers to make
 * XML access/creation a little easier
 *
 * used by:
 * lib\Helper\PHPInfoParser
 * lib\StatesInfo
 * lib\OSMap
 *
 * @package lib\Helper
 * @author Stefanius <s.kien@online.de>
 */
class ExtDOMDocument extends \DOMDocument
{
    public function
__construct() {
       
parent::__construct('1.0', 'utf-8');
    }
   
   
/**
     * select nodes specified by XPath
     * @param DOMDocument $this
     * @param DOMElement $oParent
     * @param string $strPath
     * @return DOMNodeList
     */
   
public function selectNodes($strPath, \DOMElement $oParent=null)
    {
       
$oXPath = new \DOMXPath($this);
       
$oNodelist = $oXPath->query($strPath, $oParent);
        if (
$oNodelist === false) {
           
// PHP generates warning but with no further information about the query - so give little bit more info
           
trigger_error('DOMXPath->query: malformed expression or the invalid contextnode (' . $strPath . ')', E_USER_NOTICE);
        }
        return
$oNodelist;
    }
   
   
/**
     * select first node specified by XPath
     * @param DOMDocument $this
     * @param DOMElement $oParent
     * @param string $strNode
     * @return DOMNode or null if node not found
     */
   
public function selectSingleNode($strNode, \DOMElement $oParent=null) {
       
$oNode = null;
       
$oNodelist = $this->selectNodes($strNode, $oParent);
        if (
$oNodelist !== false && $oNodelist->length > 0) {
           
$oNode = $oNodelist->item(0);
        }
        return
$oNode;
    }
   
   
/**
     * @param DOMDocument $this
     * @param DOMElement $oParent
     * @param string $strNode
     * @return string or null if node not found
     */
   
public function getNodeValue($strNode, \DOMElement $oParent=null) {
       
$strValue = null;
       
$oNode = $this->selectSingleNode($strNode, $oParent);
        if (
$oNode != null) {
           
$strValue = $oNode->nodeValue;
        }
        return
$strValue;
    }
   
   
/**
     * get the name of the root element.
     * @return string or null, if no root found
     */
   
public function getRootName() {
       
$strName = null;
        if (
$this->documentElement) {
           
$strName = $this->documentElement->nodeName;
        }
        return
$strName;
    }
   
   
/**
     * create new DOMElement and append it to given parent
     * @param DOMDocument $this
     * @param DOMElement $oParent
     * @param string $strName
     * @param string $strValue
     * @return DOMElement
     */
   
public function addChild($strName, $strValue=null, \DOMElement $oParent=null) {
       
$oChild = $this->createElement($strName, $strValue);
       
$oParent ? $oParent->appendChild($oChild) : $this->appendChild($oChild);
       
        return
$oChild;
    }
}

For more information send a message to info at phpclasses dot org.