86Stang Posted June 4, 2007 Share Posted June 4, 2007 Hi All, Using a code example posted by amitkrghosh, I've been able to almost get my XML parsing to work as I planned but need a bit of help taking it the rest of the way home. I'm an XML neophyte at best so any help here would be much appreciated! I am pulling data from a weather XML document. I've been able to modify amitkrghosh's code so that it only pulls from the weather stations that are relevant to my viewers. However, it just lists everything within that element (and its children) and doesn't allow me to show info how I want. I'm sure this is due to the foreach loop at the end of the code but I'm just not good enough to change that to allow me to show the info I want. Here's the code and a snippet of the XML document: CODE: <?php $weather = array(); $flag = ""; $count = 0; function opening_element($parser, $element, $attributes) { /* opening XML element callback function */ global $flag; if ($element == "reporting-station") $flag = $attributes["code"]; } function closing_element($parser, $element) { /* closing XML element callback function */ global $flag; if ($element == "reporting-station") $flag = ""; } function character_data($parser, $data) { /* callback function for character data */ global $flag; if ($flag == "CYEG") { global $weather; $weather[] = $data; } } $parser = xml_parser_create(); xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false); xml_set_element_handler($parser, "opening_element", "closing_element"); xml_set_character_data_handler($parser, "character_data"); $document = file("../xml/xml_threeday.xml"); foreach ($document as $line) { xml_parse($parser, $line); } xml_parser_free($parser); foreach ($weather as $name) { echo "$name <br />"; } echo "<br />"; ?> XML SNIPPET: <reporting-station code="BIRK"> <city>Reykjavik</city> <state>Iceland</state> <forecast-day> <day>Thursday</day> <high>58</high> <low>44</low> <icon-select>9</icon-select> <sky-conditions>Showers</sky-conditions> </forecast-day> <forecast-day> <day>Friday</day> <high>60</high> <low>43</low> <icon-select>10</icon-select> <sky-conditions>Rain</sky-conditions> </forecast-day> <forecast-day> <day>Saturday</day> <high>49</high> <low>48</low> <icon-select>10</icon-select> <sky-conditions>Rain</sky-conditions> </forecast-day> </reporting-station> Again, any help would be very much appreciated!!! Quote Link to comment https://forums.phpfreaks.com/topic/54139-xml-parsing-with-php/ Share on other sites More sharing options...
sKunKbad Posted June 4, 2007 Share Posted June 4, 2007 Life would be way easier for you if you are running php5 to just use simpleXML. Quote Link to comment https://forums.phpfreaks.com/topic/54139-xml-parsing-with-php/#findComment-267667 Share on other sites More sharing options...
86Stang Posted June 4, 2007 Author Share Posted June 4, 2007 I hear ya but unfortunately my webhost isn't running it yet. Such is life. Any idea on what I can do? Quote Link to comment https://forums.phpfreaks.com/topic/54139-xml-parsing-with-php/#findComment-267672 Share on other sites More sharing options...
per1os Posted June 4, 2007 Share Posted June 4, 2007 What do you get for output? ?php $weather = array(); $flag = ""; $count = 0; function opening_element($parser, $element, $attributes) { /* opening XML element callback function */ global $flag; if ($element == "reporting-station") $flag = $attributes["code"]; } function closing_element($parser, $element) { /* closing XML element callback function */ global $flag; if ($element == "reporting-station") $flag = ""; } function character_data($parser, $data) { /* callback function for character data */ global $flag; if ($flag == "CYEG") { global $weather; $weather[] = $data; } } $parser = xml_parser_create(); xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false); xml_set_element_handler($parser, "opening_element", "closing_element"); xml_set_character_data_handler($parser, "character_data"); $document = file("../xml/xml_threeday.xml"); foreach ($document as $line) { xml_parse($parser, $line); } xml_parser_free($parser); foreach ($weather as $name) { echo $name['city'] . "<br />"; } // just do a data dump to see what the $weather array is like. echo "<pre>", print_r($weather),"<br />"; ?> What does that code produce? Quote Link to comment https://forums.phpfreaks.com/topic/54139-xml-parsing-with-php/#findComment-267676 Share on other sites More sharing options...
86Stang Posted June 4, 2007 Author Share Posted June 4, 2007 Hi frost, I get the following output with that code: E C T 8 5 3 P F 8 5 3 P S 7 5 9 S Array ( [0] => Edmonton [1] => Canada [2] => Thursday [3] => 84 [4] => 59 [5] => 3 [6] => Partly cloudy [7] => Friday [8] => 83 [9] => 55 [10] => 3 [11] => Partly cloudy [12] => Saturday [13] => 77 [14] => 56 [15] => 9 [16] => Showers ) 1 Quote Link to comment https://forums.phpfreaks.com/topic/54139-xml-parsing-with-php/#findComment-267681 Share on other sites More sharing options...
per1os Posted June 4, 2007 Share Posted June 4, 2007 Ok, well the $name['city'] part is incorrect as it is not a multi-dimm array. How would you like to show the data. I would figure out how to determine a tags name and use that as the array's index to store the data. I never worked with XML functions too much but I am sure you can find already pre-built xml parsing code using google. Or even in the user contribs at www.php.net/xml Quote Link to comment https://forums.phpfreaks.com/topic/54139-xml-parsing-with-php/#findComment-267687 Share on other sites More sharing options...
86Stang Posted June 4, 2007 Author Share Posted June 4, 2007 I'd like it to show something like: Day -- High -- Low -- Sky Conditions Day -- High -- Low -- Sky Conditions I'll start hunting on Google again but when I searched before I met with very little results, especially considering I have PHP 4 and the parser needs to filter by reporting station. Quote Link to comment https://forums.phpfreaks.com/topic/54139-xml-parsing-with-php/#findComment-267697 Share on other sites More sharing options...
per1os Posted June 4, 2007 Share Posted June 4, 2007 http://www.criticaldevelopment.net/xml/parser_php4.phps That might be a good place to start. Using that properly something like this would work... echo $xmlObject->city; Unsure though, have not implemented it but seems like a really good class to use. Quote Link to comment https://forums.phpfreaks.com/topic/54139-xml-parsing-with-php/#findComment-267702 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.