Parsing XML data with php

I am trying to pull text from a single element in an xml file, i was able to pull the entire file but i really only need a couple of lines from the file...

On this page i was able to pull the entire xml file ( http://smyrnainlet.com/testing.php )

But when i try to just single out one line from that file. http://smyrnainlet.com/current_data.php

This is the error i am receiving:

Fatal error: Call to a member function getbyElementID() on a non-object in /home/content/74/8620474/html/current_data.php on line 9

If anyone could help me that would be amazing, i have been struggling with this for hours.

This is my code:

<?php

function startElemHandler($parser, $name, $attribs) 
{
    if (strcasecmp($name, "current_observation") ==0 ) {
        echo "<div id='waves'>\n";
    }
    if (strcasecmp($name, "wave_height_ft") ==0) {
            $waveHeight->getbyElementID("wave_height_ft");

            echo $waveHeight->asXML();
    }

}

function endElemHandler($parser, $name)
{ 
    if (strcasecmp($name, "current_observation") ==0 ) {
        echo "</div>";
    }

}

$parser = xml_parser_create();
xml_set_element_handler($parser, startElemHandler, endElemHandler);
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);

$strXML = implode("",file("http://www.weather.gov/xml/current_obs/41009.xml"));

xml_parse($parser, $strXML);

xml_parser_free($parser);


?>

Answers


You have a few fundamental flaws with your code but you are essentially asking how to parse an XML file. I suggest using PHP's DOMDocument with DOMXPath to extract the data you need. Here is some example code:

$xml = file_get_contents('weather.xml');
$dom = new DOMDocument();
@$dom->loadHTML($xml);
$domx = new DOMXPath($dom);
$entries = $domx->evaluate("//wave_height_ft");
$arr = array();
foreach ($entries as $entry) {
    $arr[] = '<' . $entry->tagName . '>' . $entry->nodeValue .  '</' . $entry->tagName . '>';
}
print_r($arr);

Need Your Help

Mono and ASP.NET Authentication

asp.net authentication mono

Does anyone know how to get to work the authentication mechanism configured using Web Site Administration Tool under Linux running Mono? Is it even possible?

Trying to perform a a linq query that joins two tables, has a where clause, and groups the results

sql-server linq linq-to-sql

I am sure it is a simple, but I can't quite get it to work. This is the SQL query I am using as my reference. I want to make this same thing happen with LINQ.