Last active
August 29, 2015 14:23
-
-
Save fernandojsg/9c5858c0c1d399970cc4 to your computer and use it in GitHub Desktop.
XML Utils for php
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
class XmlUtils | |
{ | |
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); | |
} | |
} | |
} | |
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 function printXml($xmlStr) | |
{ | |
print_r ( "<pre>" . htmlentities ( XmlUtils::formatXmlString($xmlStr) ) . "</pre>" ); | |
} | |
public static function formatXmlString($xml) | |
{ | |
$html_output=false; | |
$xml_obj = new SimpleXMLElement($xml); | |
$level = 4; | |
$indent = 0; // current indentation level | |
$pretty = array(); | |
// get an array containing each XML element | |
$xml = explode("\n", preg_replace('/>\s*</', ">\n<", $xml_obj->asXML())); | |
// shift off opening XML tag if present | |
if (count($xml) && preg_match('/^<\?\s*xml/', $xml[0])) { | |
$pretty[] = array_shift($xml); | |
} | |
foreach ($xml as $el) { | |
if (preg_match('/^<([\w])+[^>\/]*>$/U', $el)) { | |
// opening tag, increase indent | |
$pretty[] = str_repeat(' ', $indent) . $el; | |
$indent += $level; | |
} else { | |
if (preg_match('/^<\/.+>$/', $el)) { | |
$indent -= $level; // closing tag, decrease indent | |
} | |
if ($indent < 0) { | |
$indent += $level; | |
} | |
$pretty[] = str_repeat(' ', $indent) . $el; | |
} | |
} | |
$xml = implode("\n", $pretty); | |
return ($html_output) ? htmlentities($xml) : $xml; | |
} | |
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