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;
}
}
}
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:
global $sel_image;
$sel_image
is the function argument, here no reason to make it a global state.
"{$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']
$price = file_get_contents("{$sel_image['flipkart_content']}");
Here is no need to load the file/url separately - use DOMDocument::loadHTMLFile()