<?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;
}
}
|