note 61088 deleted from ref.domxml by sobak
| From: | sobak@php.net | Date: | Tue, 22 Nov 2016 03:42:47 +0000 |
| Subject: | note 61088 deleted from ref.domxml by sobak | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-207428@lists.php.net to get a copy of this message | ||
Note Submitter: Palatinusz - karoly_szalkary at yahoo dot com
----
My short way of parsing an XML document, for example displaying the document in a structured form:
<?php
$indent = "";
$file = "semi.xml";
$showfile = file_get_contents("c:/Program Files/Apache Group/apache/htdocs/phpxml" .
"/" . $file); // whatever path
// maybe the whole path is not important, look it up in other posts
$newstring=utf8_encode($showfile); // it's important!
if(!$domDocument = domxml_open_mem($newstring)) {
echo "Couldn't load xml...";
exit;
}
$rootDomNode = $domDocument->document_element();
print "<pre>";
printElements($rootDomNode);
print "</pre>";
function printElements($domNode)
{
if($domNode)
{
global $indent;
if($domNode->node_type() == XML_ELEMENT_NODE)
{
print "<br />".$indent."<".$domNode->node_name();
if($domNode->has_attributes())
{
$attributes = $domNode->attributes();
foreach($attributes as $domAttribute)
{
print " $domAttribute->name=\"$domAttribute->value\"";
}
}
print ">";
if($domNode->has_child_nodes())
{
$indent.=" ";
$nextNode = $domNode->first_child();
printElements($nextNode);
$indent= substr($indent, 0, strlen($indent)-2);
print "<br
/>".$indent."<"."/".$domNode->node_name().">";
}
else
{
print
"$domNode->node_value()</".$domNode->node_name().">";
}
}
$nextNode = $domNode->next_sibling();
printElements($nextNode);
}
}
?>