0

I need the SAX parser code for my DOM Document code.

If anybody would be kind enough to provide me the code for the same.

 function flipkart_price_fetch($sel_image){  
    global $sel_image;  
    $price = file_get_contents("{$sel_image['flipkart_content']}");  
    $dom = new DOMDocument();  
    @$dom->loadHTML($price);  
    $divs = $dom->getElementsByTagName('span');  
    foreach($divs as $div){  
        if($div->getAttribute('class') == 'fk-font-verybig pprice fk-bold'){  
            echo $div->nodeValue;  
            }  
        }  
    } 
Manoj Majumdar
  • 505
  • 1
  • 4
  • 23
  • Why do you think you would need to use SAX? – ThW Jun 11 '14 at 07:06
  • I have used DOM here and it is taking a heck lot of time to parse.. so I googled it and found that SAX parser is more faster than DOM. I need to get the results faster on my webpage.. what do u suggest instead? – Manoj Majumdar Jun 11 '14 at 07:18
  • I use DOM+Xpath mostly. XMLReader for really large XML files, but DOM+Xpath again for the parts. I am not sure that SAX or XMLReader can even handle HTML. – ThW Jun 11 '14 at 07:22
  • Sir, can you please provide your solution to this code to parse it faster.. I am in dying need to make it happen. – Manoj Majumdar Jun 11 '14 at 07:31

1 Answers1

0

I don't think DOM is the performance hog here, but the iteration of all span element nodes. DOMXpath allows to use Xpath expressions to fetch nodes directly:

$dom = new DOMDocument();  
@$dom->loadHTMLFile($sel_image['flipkart_content']);  
$xpath = new DOMXpath($dom);

// this matches the node by the class name "pprice"
$price = $xpath->evaluate(
  'string(.//span[contains(concat(" ", normalize-space(@class), " "), " pprice ")])'
);

echo $price;

Search SO and/or the Web for Xpath.

You have some additional errors in your source:

  1. global $sel_image;

    $sel_image is the function argument, here no reason to make it a global state.

  2. "{$sel_image['flipkart_content']}"

    This string contains only the variable, use the variable directly $sel_image['flipkart_content'] or cast it to a string (string)$sel_image['flipkart_content']

  3. $price = file_get_contents("{$sel_image['flipkart_content']}");

    Here is no need to load the file/url separately - use DOMDocument::loadHTMLFile()

Community
  • 1
  • 1
ThW
  • 19,120
  • 3
  • 22
  • 44
  • Thanks for your feedback.. I have made $sel_image global because i have also used it somewhere else in my other code.. Regarding the errors 2 and 3, I will make sure of that.. :) – Manoj Majumdar Jun 11 '14 at 11:00