Jump to content

XML Parsing with PHP


86Stang

Recommended Posts

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!!!

Link to comment
https://forums.phpfreaks.com/topic/54139-xml-parsing-with-php/
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/54139-xml-parsing-with-php/#findComment-267676
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/54139-xml-parsing-with-php/#findComment-267681
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/54139-xml-parsing-with-php/#findComment-267687
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/54139-xml-parsing-with-php/#findComment-267697
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.