PHP Classes

File: lib/Helper/ExtDOMDocument.php

Recommend this page to a friend!
  Classes of Stefan Kientzler   PHP Holidays Library for Germany, Austria and Switzerland   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: PHP Holidays Library for Germany, Austria and Switzerland
Get the holiday dates of specific countries
Author: By
Last change:
Date: 4 years ago
Size: 1,789 bytes
 

Contents

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

/**
 * a few little helpers to make access to XML data a little easier
 */
class ExtDOMDocument extends \DOMDocument
{
   
/**
     * 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);
        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->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;
    }
   
   
/**
     * 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;
    }
}