<?php
$string = <<<XML
<a>
 <b>
  <c>text</c>
  <c>stuff</c>
 </b>
 <d>
  <c>code</c>
 </d>
</a>
XML;
$xml = new SimpleXMLElement($string);
echo $xml->asXML();
$result = $xml->xpath('/a/b/c');

while(list( ,$node) = each($result))
{
    echo $node->asXML($result);
}

need this output:
<c>text</c>
<c>stuff</c>
<>code</c>

$string = <<<XML
<a>
<b>
<c>text</c>
<c>stuff</c>
</b>
<d>
<c>code</c>
</d>
</a>
XML;
$xml = new SimpleXMLElement($string);
echo $xml->asXML();
$result = $xml->xpath('/a/b/c');

foreach ($result as $id => $child) {
  echo "<c>".(string)$child."</c>";

}
<?php
$string = <<<XML
<a>
<b>
<c>text</c>
<c>stuff</c>
</b>
<d>
<c>code</c>
</d>
</a>
XML;
$xml = new SimpleXMLElement($string);
echo $xml->asXML();
$result = $xml->xpath('/a/b/c');

foreach ($result as $id => $child) {
    echo "<c>".(string)$child."</c>";

}

?>

while using this code i got this type of error message..
The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.

--------------------------------------------------------------------------------

Only one top level element is allowed in an XML document. Error processing resource 'http://127.0.0.1:33987/sample.php?XDEB...

<c>text</c><c>stuff</c>
-^

Try this

<?php
$string = <<<XML
<a>
<b>
<c>text</c>
<c>stuff</c>
</b>
<d>
<c>code</c>
</d>
</a>
XML;
$xml = new SimpleXMLElement($string);
 $xml->asXML()."<br>";


    //echo "<c>".(string)$child."</c>";
    foreach( $xml->children() AS $child )
{
    //run any query you want on the children.. they are also nodes.
   $name1 = $child;
   //echo "<pre><c></pre>".$name1."<pre><c></pre><br>";
   foreach( $name1->children() AS $child1 )
{
     $name2 = $child1->getName();
     $child2=$child1;
    // echo $name2."--".$child2;
        echo "&lt;".$name2."&gt;".$child2."&lt;&frasl;".$name2."&gt;"."<br>";

}

}

?>

i need particular node to retrieve.. that is <c>code</c> means what should i do..?

try this

<?php
$string = <<<XML
<a>
<b>
<c>text</c>
<c>stuff</c>
</b>
<d>
<c>code</c>
</d>
</a>
XML;
$xml = new SimpleXMLElement($string);
 $xml->asXML()."<br>";


    //echo "<c>".(string)$child."</c>";
    foreach( $xml->children() AS $child )
{
    //run any query you want on the children.. they are also nodes.
   $name1 = $child;
   //echo "<pre><c></pre>".$name1."<pre><c></pre><br>";
   foreach( $name1->children() AS $child1 )
{
     $name2 = $child1->getName();
     $child2=$child1;
    // echo $name2."--".$child2;
    if($child2=="code")
    {
        echo "&lt;".$name2."&gt;".$child2."&lt;&frasl;".$name2."&gt;"."<br>";
    }

}

}

thank you.. but not like that.. if b node has value like <b id="tp01"> means it returns b child nodes..

echo nl2br(htmlentities(getChildXML($string, 'b', 'tp001')));
why linebreak is not working here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.