private function CreateDomDocument() { $dom = new Domdocument(); @$dom->loadHTML($this->htmlStack); $xpath = new DomXpath($dom); // Find all the product blocks that contain beer attribute that we want to search for such as rating, title, price, etc. $elements = $xpath->query('//article[contains(@class, "' . $this->class . '")]'); foreach ($elements as $element) { // get the node value from the .title class (the title of the product) $title = $xpath->query('descendant::div[@class="title"]', $element); $title = $title->item(0)->nodeValue; // get the attribute value from the .link-overlay class (the actual link to the product) $link = $xpath->query('descendant::a[@class="link-overlay"]', $element); $link = $link->item(0)->getAttribute('href'); $link = 'https://www.gall.nl' . $link; // get the image src value to rip the image from the site $image = $xpath->query('descendant::div[@class="image"]/node()/node()', $element); $image = $image->item(1)->getAttribute('src'); // Dive in the link that a single product has $results = $this->curlGetDeep($link); // if a regex option has been supplied run it on the results if ($this->regex) { $title = preg_replace($this->regex, '', $title); } // remove the unwanted 6X 35x6(bulk beers) results with this regex if (!preg_match('/\\dX(\\d+)?/', $title)) { $this->elementsArray[] = ['title' => $title, 'link' => $link, 'image' => $image, 'description' => $results['description'], 'abv' => $results['abv']]; } } $this->json = json_encode(['beers' => $this->elementsArray]); }
public function CreateDomDocument() { $dom = new Domdocument(); @$dom->loadHTML($this->htmlStack); $xpath = new DomXpath($dom); $elements = $xpath->query('//a[@class="' . $this->class . '"]'); foreach ($elements as $element) { $element = $element->getAttribute('href'); // if a regex option has been supplied run it on the results if ($this->regex) { $element = preg_replace($this->regex, '', $element); } // remove the 6X 35x6(bulk beers) results with this regex if (!preg_match('/\\dX(\\d+)?/', $element)) { $this->elementsArray[] = ['title' => $element]; } } echo json_encode(['beers' => $this->elementsArray]); }
7.创建goods节点 8.把name,intro放入goods节点 9.创建属性节点goods_id 10.把属性节点放入goods节点 11.创建appstore节点 12.把goods放入appstore节点 13.把appstore放入文档中 在上面的步骤中,体现出: 创建普通节点 创建CDATA节点 创建属性节点 增加子节点 */ //创建DOM文档对象 $dom = new Domdocument('1.0', 'utf-8'); //创建文本节点 $tl = $dom->createTextNode('天龙八部'); //创建普通节点 $name = $dom->createElement('name'); //在把天龙文本节点加入到name节点中 $name->appendChild($tl); //创建cdata节点 $cdata = $dom->createCDATASection('天龙八部~~~~'); //再创建intro节点 $intro = $dom->createElement('intro'); //再把cdata节点放入intro中 $intro->appendChild($cdata); //创建goods节点 $goods = $dom->createElement('goods'); //把name,intro放入goods节点
/** * Api function for crawl bank daily data. * * @return Response */ public function getBankDaily() { $apies = json_decode(EXR_BANKS); // Crawl bank api url. foreach ($apies as $type => $url) { switch ($type) { case 'VCB': $content = file_get_contents($url); if (!$content) { return false; } $doc = new \Domdocument(); @$doc->loadxml($content); $xpath = new \Domxpath($doc); $items = $xpath->query('//Exrate'); $codes = array(); foreach ($items as $item) { $codes[] = $item->getattribute('CurrencyCode'); } // get currency by ids $currencies = Currency::getCurrenciesByCodes($codes, $type); // get exchange rates over bank api. if (count($currencies)) { foreach ($currencies as $currency) { $item = $xpath->query('//Exrate[@CurrencyCode="' . $currency->code . '"]')->item(0); $currencyid = $currency->id; $buy = $item->getattribute('Buy'); $transfer = $item->getattribute('Transfer'); $sell = $item->getattribute('Sell'); if ($buy && $transfer && $sell) { CurrencyDaily::updateExratesDaily($currencyid, $buy, $transfer, $sell, 'bank'); } } } break; } } return Response::json(array('status' => 'Success', 'data' => $apies), 200); }
<?php /* * $key = abe95e495c0820cc; * echo $api,得到的是一个XML文档,内容是视频的标题,时长,封面等等信息。接下来,我们要做的是用PHP把 * XML的各节点信息解析出来。 * * 知识:DOMDocument类来解析 * * 当前,我们暂时不用XML解析,而是直接用字符串操作来得到地址 */ error_reporting(E_ALL & ~E_NOTICE); $key = 'abe95e495c0820cc'; if ($tudou = $_POST['tudou']) { $api = 'http://api.tudou.com/v3/gw?method=item.info.get&appKey=' . $key . '&format=xml&itemCodes=' . $itemCode; $dom = new Domdocument('1.0', 'utf-8'); $dom->load($api); $noad = $dom->getElementsByTagName('html5Url')->item(0)->nodeValue; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>xml历史及特点</title> </head> <body> <pre> 第28集:http://www.tudou.com/albumplay/bHQgbXXsD-w/vSiti0Uvtgc.html </pre>
<?php /* DOM操作XML的节点读取,节点创建与删除 节点能不能修改与删除 */ //创建DOM对象 $dom = new Domdocument('1.0', 'utf-8'); //载入xml $dom->load('./035.xml'); //把天龙八部的商品删除 //查找goods节点,得到列表,从列表中选取天龙八部节点 $tl = $dom->getElementsByTagName('goods')->item(0); $tl->parentNode->removeChild($tl); //删除节点以及搞定 /* header('content-type:text/xml;charset=utf-8'); echo $dom->savexml(); */ //修改节点,节点不能修改,只能替换 $name = $dom->getElementsByTagName('name')->item(0); $text = $dom->createTextNode('电视剧'); $name->replaceChild($text, $name->firstChild); header('content-type:text/xml;charset=utf-8'); echo $dom->savexml();