从XML表拉NHL排名与PHP [英] Pulling NHL Standings from XML Table with PHP
问题描述
我的工作中,我拉对NHL的各种统计数据的一个项目,并将其插入到SQL表。 presently,我工作的拼抢阶段,并且已经发现,我实现了一个XML解析器,但我不能为我的生活弄清楚如何从中提取信息。该表可以在这里找到 - > http://www.tsn.ca/datafiles /XML/NHL/standings.xml 。
解析器理应产生一个多dimmensional数组,我只是想拉的所有统计数据从信息队一节,但我不知道如何把从阵列的信息。我怎么会去拉胜蒙特利尔有多少? (全数作为统计的其余部分的例子)
这就是当前页面看起来像 - > http://mattegener.me/school/standings.php一>
这里的code:
< PHP
$ strYourXML =http://www.tsn.ca/datafiles/XML/NHL/standings.xml;
$跳频=的fopen($ strYourXML,'R');
$哑=与fgets($ FH);
$内容='';
而($行=与fgets($ FH))$内容= $行。
FCLOSE($ FH);
$ objXML =新xml2Array();
$ arrOutput = $ objXML->解析($内容);
的print_r($ arrOutput [0]); //这个打印输出数组。类xml2Array {变量$ arrOutput =阵列();
变量$ resParser;
变量$ strXmlData;功能解析($ strInputXML){ $这个 - > resParser = xml_parser_create();
xml_set_object($这个 - > resParser,$本);
xml_set_element_handler($这 - > resParsertagOpen,tagClosed); xml_set_character_data_handler($这个 - > resParser,tagdata进行); $这个 - > strXmlData = xml_parse($这个 - > resParser,$ strInputXML);
如果($这个 - >!strXmlData){
死亡(sprintf的(XML错误:%至%d行的,
xml_error_string(xml_get_error_ code($这个 - > resParser))
xml_get_current_line_number($这 - > resParser)));
} xml_parser_free($这个 - > resParser); 返回$这个 - > arrOutput;
}
功能tagOpen($解析器,$名称,$ ATTRS){
$标签=阵列(名= GT; $名字,ATTRS=> $ ATTRS);
array_push($这个 - > arrOutput,$标记);
}功能tagdata进行($解析器,$ tagdata进行){
如果(修剪($ tagdata进行)){
如果(使用isset($这个 - > arrOutput [计数($这个 - > arrOutput)-1] ['tagdata进行'])){
$这个 - > arrOutput [计数($这个 - > arrOutput)-1] ['tagdata进行'] = $ tagdata进行。
}
其他{
$这个 - > arrOutput [计数($这个 - > arrOutput)-1] ['tagdata进行'] = $ tagdata进行;
}
}
}功能tagClosed($解析器,$名){
$这个 - > arrOutput [计数($这个 - > arrOutput)-2] ['孩子'] [] = $这个 - > arrOutput [计数($这个 - > arrOutput)-1];
array_pop($这个 - > arrOutput);
}
}
?>
添加此搜索功能,你的类以及与此code玩
$ objXML =新xml2Array();
$ arrOutput = $ objXML->解析($内容);
//第一个参数始终为0
//第二个是'儿童',除非你需要像上次更新最新信息
//第三个是你想要的例子统计范畴
// 6 =>你想要的阵列上战绩
的print_r($ arrOutput [0] ['孩子'] [6]);
使用搜索功能//如果键名是蒙特利尔全阵列
//结果将是montreals阵列
$ search_result = $ objXML->搜索($ arrOutput,NAME,蒙特利尔');
//第一个参数始终为0
//二是密钥名
回声$ search_result [0] ['WINS'];功能搜索($数组$关键,$值)
{
$结果=阵列(); 如果(is_array($阵列))
{
如果(使用isset($数组[$关键])及和放大器; $阵列[$关键] == $值)
$结果[] = $阵列; 的foreach($数组作为子阵$)
$结果= array_merge($结果,这 - $>搜索($子数组$键,$值));
} 返回$结果;
}
这个搜索功能是区分大小写的,它需要像火柴修改
百分比在蒙特利尔键或值改为大写M为小写将是空
I'm working on a project in which I pull various statistics about the NHL and inserting them into an SQL table. Presently, I'm working on the scraping phase, and have found an XML parser that I've implemented, but I cannot for the life of me figure out how to pull information from it. The table can be found here -> http://www.tsn.ca/datafiles/XML/NHL/standings.xml. The parser supposedly generates a multi-dimmensional array, and I'm simply trying to pull all the stats from the "info-teams" section, but I have no idea how to pull that information from the array. How would I go about pulling the number of wins Montreal has? (Solely as an example for the rest of the stats) This is what the page currently looks like -> http://mattegener.me/school/standings.php here's the code:
<?php
$strYourXML = "http://www.tsn.ca/datafiles/XML/NHL/standings.xml";
$fh = fopen($strYourXML, 'r');
$dummy = fgets($fh);
$contents = '';
while ($line = fgets($fh)) $contents.=$line;
fclose($fh);
$objXML = new xml2Array();
$arrOutput = $objXML->parse($contents);
print_r($arrOutput[0]); //This print outs the array.
class xml2Array {
var $arrOutput = array();
var $resParser;
var $strXmlData;
function parse($strInputXML) {
$this->resParser = xml_parser_create ();
xml_set_object($this->resParser,$this);
xml_set_element_handler($this->resParser, "tagOpen", "tagClosed");
xml_set_character_data_handler($this->resParser, "tagData");
$this->strXmlData = xml_parse($this->resParser,$strInputXML );
if(!$this->strXmlData) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($this->resParser)),
xml_get_current_line_number($this->resParser)));
}
xml_parser_free($this->resParser);
return $this->arrOutput;
}
function tagOpen($parser, $name, $attrs) {
$tag=array("name"=>$name,"attrs"=>$attrs);
array_push($this->arrOutput,$tag);
}
function tagData($parser, $tagData) {
if(trim($tagData)) {
if(isset($this->arrOutput[count($this->arrOutput)-1]['tagData'])) {
$this->arrOutput[count($this->arrOutput)-1]['tagData'] .= $tagData;
}
else {
$this->arrOutput[count($this->arrOutput)-1]['tagData'] = $tagData;
}
}
}
function tagClosed($parser, $name) {
$this->arrOutput[count($this->arrOutput)-2]['children'][] = $this->arrOutput[count($this- >arrOutput)-1];
array_pop($this->arrOutput);
}
}
?>
add this search function to your class and play with this code
$objXML = new xml2Array();
$arrOutput = $objXML->parse($contents);
// first param is always 0
// second is 'children' unless you need info like last updated date
// third is which statistics category you want for example
// 6 => the array you want that has wins and losses
print_r($arrOutput[0]['children'][6]);
//using the search function if key NAME is Montreal in the whole array
//result will be montreals array
$search_result = $objXML->search($arrOutput, 'NAME', 'Montreal');
//first param is always 0
//second is key name
echo $search_result[0]['WINS'];
function search($array, $key, $value)
{
$results = array();
if (is_array($array))
{
if (isset($array[$key]) && $array[$key] == $value)
$results[] = $array;
foreach ($array as $subarray)
$results = array_merge($results, $this->search($subarray, $key, $value));
}
return $results;
}
Beware
this search function is case sensitive it needs modifications like match to
a percentage the key or value changing capital M in montreal to lowercase will be empty
这篇关于从XML表拉NHL排名与PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!