PHP DOM Document PLUS

This class adds a few node functions to the DOM Document class

class domplus extends DOMDocument {

function domnodecreate($xpath, $nname, $aname=null, $avalue=null) {
   
    $xp = new DOMXPath($this);
   
    $n = $this->createElement($nname);
   
    if (!is_null($aname) or !is_null($avalue)) {
        $n->setAttribute($aname,$avalue);
    }
   
    $parent = $xp->query($xpath)->item(0);
    $parent->appendChild($n);
}

function domnodeset($xpath, $value){

    $xp = new DOMXPath($this);
    foreach($xp->query($xpath) as $n) {
        $nodeType = $n->nodeType;
        if($nodeType == XML_ELEMENT_NODE)
            $n->nodeValue=$value;
        elseif($nodeType == XML_ATTRIBUTE_NODE)
            $n->value=$value;
    }
}

function domnodeget($xpath) {
   
    $xp = new DOMXPath($this);
    $n = $xp->query($xpath)->item(0);
    $nodeType = $n->nodeType;
    if($nodeType == XML_ELEMENT_NODE)
        $value=$n->nodeValue;
    elseif($nodeType == XML_ATTRIBUTE_NODE)
        $value=$n->value;
    return $value;
}

function domnoderemove($xpath) {
   
    $xp = new DOMXPath($this);
    foreach($xp->query($xpath) as $n) {
        $nodeType = $n->nodeType;
        if($nodeType == XML_ELEMENT_NODE)
            $n->parentNode->removeChild($n);
        elseif($nodeType == XML_ATTRIBUTE_NODE)
            $n->ownerElement->removeAttributeNode($n);
    }
}

function domnodedateshorten($xpath) {

    // convert "long dates" into string "short dates"

    $xp = new DOMXPath($this);
    foreach($xp->query($xpath) as $n) {
        $date = new DateTime($n->nodeValue);
        $n->nodeValue = $date->format('Y-m-d');
    }
}

}



REFERRERS
PhpXml
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki