Created
July 19, 2013 11:16
-
-
Save fernandojsg/6038435 to your computer and use it in GitHub Desktop.
PHP: XML Nodes help functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function replaceNode(&$srcNode, &$dstNode,&$xmlDoc) | |
{ | |
$newNode = $xmlDoc->importNode($srcNode, true); | |
$dstNode->parentNode->replaceChild($newNode,$dstNode); | |
} | |
function importNode(&$newNode, &$refNode,&$xmlDoc) | |
{ | |
if ($newNode->childNodes) | |
foreach ($newNode->childNodes as $child) | |
{ | |
$newNode2 = $xmlDoc->importNode($child, true); | |
insertNode($newNode2, $refNode, 'inside'); | |
} | |
} | |
function removeChildren(&$node) | |
{ | |
while ($node->firstChild) | |
{ | |
while ($node->firstChild->firstChild) | |
{ | |
removeChildren($node->firstChild); | |
} | |
$node->removeChild($node->firstChild); | |
} | |
} | |
function removeNode(&$node) | |
{ | |
removeChildren($node); | |
$node->parentNode->removeChild($node); | |
} | |
function insertNode(&$newNode, &$refNode, $insertMode='inside') | |
{ | |
if(!$insertMode || $insertMode == "inside") | |
{ | |
/* $found=false; | |
foreach ($refNode->childNodes as $child) | |
{ | |
if ($newNode->tagName == $child->tagName) | |
$refNode->removeChild($child); | |
} | |
if ($found) | |
return; | |
*/ | |
$refNode->appendChild($newNode); | |
} | |
else if($insertMode == "before") | |
{ | |
$refNode->parentNode->insertBefore($newNode, $refNode); | |
} | |
else if($insertMode == "after") | |
{ | |
if($refNode->nextSibling) { | |
$refNode->parentNode->insertBefore($newNode, $refNode->nextSibling); | |
} else { | |
$refNode->parentNode->appendChild($newNode); | |
} | |
} | |
} | |
class XmlUtils | |
{ | |
public static function removeChildren(&$node) | |
{ | |
while ($node->firstChild) | |
{ | |
while ($node->firstChild->firstChild) | |
{ | |
XmlUtils::removeChildren($node->firstChild); | |
} | |
$node->removeChild($node->firstChild); | |
} | |
} | |
public static function insertNode($newNode, $refNode, $insertMode=null) | |
{ | |
if(!$insertMode || $insertMode == "inside") | |
{ | |
$refNode->appendChild($newNode); | |
} | |
else if($insertMode == "before") | |
{ | |
$refNode->parentNode->insertBefore($newNode, $refNode); | |
} | |
else if($insertMode == "after") | |
{ | |
if($refNode->nextSibling) { | |
$refNode->parentNode->insertBefore($newNode, $refNode->nextSibling); | |
} else { | |
$refNode->parentNode->appendChild($newNode); | |
} | |
} | |
} | |
public static function addTagWithTabs(&$str, $value, $level=0) | |
{ | |
if (is_array($level)) | |
$level=count($level); | |
$level=$level+1; | |
//$str.=str_repeat(CHR(9),$level).$value.CHR(10); | |
$str .= str_repeat ( " ", $level ) . $value . CHR ( 10 ); | |
} | |
public static function printXml($xmlStr) | |
{ | |
print_r ( "<pre>" . htmlentities ( $xmlStr ) . "</pre>" ); | |
} | |
public static function getElementById(&$nodeRoot, &$xmlDoc, $id) | |
{ | |
$value=$nodeRoot->getAttribute("id"); | |
if ($nodeRoot->hasAttribute("id") && (!strcmp($value,$id))) | |
{ | |
return $nodeRoot; | |
} | |
if ($nodeRoot->hasChildNodes()) | |
{ | |
foreach($nodeRoot->childNodes as $child) | |
{ | |
if (get_class($child)=='DOMElement') | |
{ | |
$node=XmlUtils::getElementById($child,$xmlDoc,$id); | |
if ($node!=NULL) | |
return $node; | |
} | |
} | |
} | |
return NULL; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment