note 80985 deleted from book.dom by cmb
| From: | cmb@php.net | Date: | Sun, 15 Sep 2019 18:05:50 +0000 |
| Subject: | note 80985 deleted from book.dom by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212226@lists.php.net to get a copy of this message | ||
Note Submitter: ptichy at pobox dot sk
----
How to get contents of node with all tags by PHP Dom:
<?php
// XML data
$xml_string = "<?xml version='1.0' ?>
<sentence>
How to get contents of node with all tags by PHP Dom: <br/>
<b>cabbages</b>, <i>tomatoes</i>,
<i>apples</i>, <font color='purple'>aubergines</font>
<ol>
<li> row 1</li>
<li> row 2 </li>
<li> row 3 </li>
</ol>
<table border = '1'>
<tbody>
<tr>
<td>I am best</td>
</tr>
<tr>
<td>Programmer</td>
</tr>
</tbody>
</table>
You see my projects at:
<strong><a href='http://www.itdeveloping.eu'
>www.itdeveloping.eu</a></strong>
</sentence>";
// parse it
$doc= new DOMDocument();
if (!$doc->loadXML($xml_string))
{
echo ("Error in XML document");
}
$nody = $doc->getElementsByTagName('sentence'); // gets NodeList
$nod=$nody->item(0);//Node
getContent($Content,$nod);
echo $Content;
function getContent(&$NodeContent="",$nod)
{ $NodList=$nod->childNodes;
for( $j=0 ; $j < $NodList->length; $j++ )
{ $nod2=$NodList->item($j);//Node j
$nodemane=$nod2->nodeName;
$nodevalue=$nod2->nodeValue;
if($nod2->nodeType == XML_TEXT_NODE)
$NodeContent .= $nodevalue;
else
{ $NodeContent .= "<$nodemane ";
$attAre=$nod2->attributes;
foreach ($attAre as $value)
$NodeContent .="{$value->nodeName}='{$value->nodeValue}'" ;
$NodeContent .=">";
getContent($NodeContent,$nod2);
$NodeContent .= "</$nodemane>";
}
}
}
?>