0
<?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>

5
Contributors
8
Replies
90
Views
3 Years
Discussion Span
Last Post by briele
0
$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>";

}
0
<?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>
-^

0

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>";

}

}

?>
0

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>";
    }

}

}
1

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

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.