mattegener mattegener - 2 years ago 95
SQL Question

Pulling NHL Standings from XML Table with PHP

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 ->
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 ->
here's the code:

$strYourXML = "";
$fh = fopen($strYourXML, 'r');
$dummy = fgets($fh);
$contents = '';
while ($line = fgets($fh)) $contents.=$line;
$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_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",


return $this->arrOutput;
function tagOpen($parser, $name, $attrs) {

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];


Answer Source

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
//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;

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download