XML Parsing 101 (Someone please help)
Posted: Sat Dec 04, 2004 12:28 pm
Hi again:
Here is the code I have developed with the help from my former topic "parsing XML from weather.com".
To start out with here is the XML I am trying to parse:
for review: I am only interested in the data between the <cc> tags.
In the end, when this is parsed, I need to put the following info into variables for use in my webpage (showing the tags that contain the data for simplicity, they may be referenced for above for the data).
$temp = <tmp>
$feelsLike = <flik>
$wIcon = <icon>
$condition = <t> (t as in tom)
$windSpeed = <wind><s>
$windDir = <wind><t>
$pressure = <bar><r>
$barometer = <bar><d>
$uvIndex = <uv><i>
$uvRisk = <uv><t>
ok that being laid out, here is the code I am working with,I am not sure of any of it other than the parser is setup correctly via the documentation and the callback functions are setup correctly, what is inside may be the prob... and I have no idea how to make the "inElement" function get us the tags for the stuff I am wanting, that is where I need the help and need assurance that the other functions have the proper code in them.
Here is the code that I am using:
I am very new to PHP and OOP as I have stated, so I am struggling a lot, all your help is VERY appreciated!
Here is the code I have developed with the help from my former topic "parsing XML from weather.com".
To start out with here is the XML I am trying to parse:
Code: Select all
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--This document is intended only for use by authorized licensees of The Weather Channel. Unauthorized use is prohibited. Copyright 1995-2004, The Weather Channel Enterprises, Inc. All Rights Reserved.-->
<weather ver="2.0">
<head>
<locale>en_US</locale>
<form>MEDIUM</form>
<ut>F</ut>
<ud>mi</ud>
<us>mph</us>
<up>in</up>
<ur>in</ur>
</head>
<loc id="66214">
<dnam>Shawnee Mission, KS (66214)</dnam>
<tm>12:19 PM</tm>
<lat>38.96</lat>
<lon>-94.71</lon>
<sunr>7:22 AM</sunr>
<suns>4:56 PM</suns>
<zone>-6</zone>
</loc>
<lnks type="prmo">
<link pos="1">
<l>http://www.weather.com/outlook/health/allergies/66214?par=xoap</l>
<t>Pollen Reports</t>
</link>
<link pos="2">
<l>http://www.weather.com/outlook/travel/flights/citywx/66214?par=xoap</l>
<t>Airport Delays</t>
</link>
<link pos="3">
<l>http://www.weather.com/outlook/events/special/result/66214?when=thisweek&par=xoap</l>
<t>Special Events</t>
</link>
<link pos="4">
<l>http://www.weather.com/services/desktop.html?par=xoap</l>
<t>Download Desktop Weather</t>
</link>
</lnks>
<cc>
<lsup>12/4/04 11:53 AM CST</lsup>
<obst>Olathe Executive, KS</obst>
<tmp>54</tmp>
<flik>54</flik>
<t>Fair</t>
<icon>34</icon>
<bar>
<r>29.92</r>
<d>falling</d>
</bar>
<wind>
<s>16</s>
<gust>25</gust>
<d>230</d>
<t>SW</t>
</wind>
<hmid>43</hmid>
<vis>10.0</vis>
<uv>
<i>2</i>
<t>Low</t>
</uv>
<dewp>32</dewp>
<moon>
<icon>21</icon>
<t>Waning Gibbous</t>
</moon>
</cc>
</weather>In the end, when this is parsed, I need to put the following info into variables for use in my webpage (showing the tags that contain the data for simplicity, they may be referenced for above for the data).
$temp = <tmp>
$feelsLike = <flik>
$wIcon = <icon>
$condition = <t> (t as in tom)
$windSpeed = <wind><s>
$windDir = <wind><t>
$pressure = <bar><r>
$barometer = <bar><d>
$uvIndex = <uv><i>
$uvRisk = <uv><t>
ok that being laid out, here is the code I am working with,I am not sure of any of it other than the parser is setup correctly via the documentation and the callback functions are setup correctly, what is inside may be the prob... and I have no idea how to make the "inElement" function get us the tags for the stuff I am wanting, that is where I need the help and need assurance that the other functions have the proper code in them.
Here is the code that I am using:
Code: Select all
$data = preg_replace("/\<\?xml.*?\>/", '', $data);
$data = trim($data);
// variable array to hold data
$tagdata = array();
// Parse XML Here
function startElement($parser, $name, $attrs)
{
global $tags;
global $tagdata;
if($name == 'cc')
{
$tags = array();
}
}
// callback function when a tag is closed
function endElement($parser, $name)
{
global $tags;
global $tagdata;
if($name == 'cc')
{
array_push($tagdata, $tags); // not sure of this
}
}
// callback function when reading data between opening and closing tag
function inElement($parser, $data)
{
global $tags;
global $tagdata;
//not sure what goes here
}
// now parse
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "inElement");
xml_parse($xml_parser, $data);
xml_parser_free($xml_parser);