2
\$\begingroup\$
<?php 
$xml = simplexml_load_file("http://www.example.com/example.xml");
 //echo "<pre>";
 //print_r($xml);
 $cnt =0;
 $customereview='';
 foreach($xml->comment as $comment){
 if($comment->ProdNid == $productID){
 if($comment->PostedOn != '' && $comment->PostedBy != '' && $comment->comment_text != ''){
 if($cnt < 5) { 
 $cnt++; 
 $customereview.="<div class='review_wrp'><span class='review_product'>".$title."</span><span class='review'>".(string)$comment->comment_text."</span><span class='review_person'>".(string)$comment->PostedBy.",".(string)$comment->PostedOn ."</span>
 </div>";
 if($cnt==5)
 {
 break;
 }
 }
 }//end of if
 }
 }
 echo $customereview;
 ?>
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Aug 13, 2014 at 6:34
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

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.

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

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

answered Aug 13, 2014 at 8:01
\$\endgroup\$
2
  • \$\begingroup\$ Thanks for your quick answer response. but it is not loading fast as i have to read 10 mb Xml file. \$\endgroup\$ Commented Aug 13, 2014 at 11:58
  • \$\begingroup\$ sachin, do you need code to download 10Mb files faster or code to parse the XML faster? The code posted by @200_success is faster than your version at parsing and displaying. You should accept his answer. You never mentioned in your question the network/download penalty. \$\endgroup\$ Commented Aug 19, 2014 at 10:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.