Faster way to render XML (10MB) file, loop the content and display output &lt;?php 
$xml = simplexml_load_file("http://www.example.com/example.xml");
            //echo "&lt;pre&gt;";
            //print_r($xml);
            $cnt =0;
            $customereview='';
            foreach($xml-&gt;comment as $comment){
                    if($comment-&gt;ProdNid == $productID){
                        if($comment-&gt;PostedOn != '' &amp;&amp; $comment-&gt;PostedBy != '' &amp;&amp; $comment-&gt;comment_text != ''){
                            if($cnt &lt; 5) { 
                            $cnt++;         
                            $customereview.="&lt;div class='review_wrp'&gt;&lt;span class='review_product'&gt;".$title."&lt;/span&gt;&lt;span class='review'&gt;".(string)$comment-&gt;comment_text."&lt;/span&gt;&lt;span class='review_person'&gt;".(string)$comment-&gt;PostedBy.",".(string)$comment-&gt;PostedOn ."&lt;/span&gt;
                        &lt;/div&gt;";
                                if($cnt==5)
                                {
                                    break;
                                }
                            }

                        }//end of if
                    }
                }

                echo $customereview;
 ?&gt;
There is no error handling.  Loading a remote resource is somewhat likely to fail.  Unfortunately, simplexml doesn't make error-handling easy.

Instead of filtering the XML using PHP code, I suggest selecting exactly the nodes you want using an XPath query.  The XPath library is more likely to be optimized than your PHP code.  In any case, it is certainly simpler to maintain one string describing the relevant nodes than a bunch of nested PHP statements.

I've chosen to use PHP templating for rendering, as it is easier to see that it is output.

&lt;?php
$xml = simplexml_load_file('http://www.example.com/example.xml');
foreach ($xml-&gt;xpath("//comment[ProdNid = '" . htmlspecialchars($productId) .
                     "'][PostedOn][PostedBy][comment_text][position() &lt;= 5]") as $comment) {
?&gt;
    &lt;div class='review_wrp'&gt;
      &lt;span class='review_product'&gt;&lt;?php echo htmlspecialchars($title);
      ?&gt;&lt;/span&gt;&lt;span class='review'&gt;&lt;?php echo htmlspecialchars($comment-&gt;comment_text);
      ?&gt;&lt;/span&gt;&lt;span class='review_person'&gt;&lt;?php echo htmlspecialchars($comment-&gt;PostedBy);
      ?&gt;,&lt;?php echo htmlspecialchars($comment-&gt;PostedOn); ?&gt;&lt;/span&gt;
    &lt;/div&gt;
&lt;?
}


All of those output strings probably need to be escaped to prevent, for example, cross-site scripting.  I've used htmlspecialchars() for that.