XML Parsing 101 (Someone please help)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

XML Parsing 101 (Someone please help)

Post by irishmike2004 »

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:

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

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)
&#123;
  global $tags;
  global $tagdata;
  
  if($name == 'cc')
  &#123;
  	$tags = array();
  &#125;
&#125;

// callback function when a tag is closed
function endElement($parser, $name)
&#123;
  global $tags;
  global $tagdata;
  
  if($name == 'cc')
  &#123;
  	array_push($tagdata, $tags); // not sure of this
  &#125;
&#125;

// callback function when reading data between opening and closing tag
function inElement($parser, $data)
&#123;
  global $tags;
  global $tagdata;
  
  //not sure what goes here
&#125;

// 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);
I am very new to PHP and OOP as I have stated, so I am struggling a lot, all your help is VERY appreciated!
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

Post by irishmike2004 »

I have read over that document, and I am not sure how it will help in this instance, this is why I put the XML, I need some solid examples.

Thanks
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

Forgot this in the original posting to this thread

Post by irishmike2004 »

The file is read from the weather.com sight using this code:

so $data contains the file contents already!

Code: Select all

$data = file_get_contents('http://xoap.weather.com/weather/local/66214?cc=*&prod=xoap&link=xoap&par='.$partnerno.'&key='.licensekey.'&unit=s');
with this in mind, that is why the documentation examples don't help too much

Thanks,
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

well, as you are unable/unwilling/whatever to parse the xml, i suggest one of the following options:

1-) get php5 and you use http://www.php.net/simplexml

2-) get someone to do it for you

3-) make your brain work :P
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

Post by irishmike2004 »

I am using PHP 5... but I am brand new to both PHP and OOP, I am not understanding the parsing or how to make it work with MY XML file, I have tried your example and it doesn't work. I need a clear example of how to make it work with my data.

I do appreciate the help, but no one is giving me an example that shows me how to do this in depth... just redirecting to re-read the documents which make little sense to me.

Thanks,

Everyone has to start somewhere :-)
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

the reason why people are reluctant to show you is because most likely,
once someone shows you how to start, there will right away be a
new thread w/ you asking what to do next.

your question is not
"im having a parse error on line 13 in the folowing code, why?"

its
"how do i parse this xml?"

your basically asking people to write the script for you, more or less.
you will get answers if you post this in the barter forum and offer people money

try using google for "xml php tutorials"

you will then, LEARN how to do it, opposed to being shown how to do it.
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

Post by irishmike2004 »

thanks rehfeld, believe it or not, your suggestion is the most helpful so far :-)

yeah, I would like to see exactly how it works because that will help me to understand what I am doing here, I wouldn't be so on edge if I hadn't spent the last 2 days trying to figure this out and even looking at a script that was written for people to use freely that parses XML, but it is for a specific XML file that this is different from so obviously it doesn't work as scripted.

anyhow, I appreciate the tutorial suggestion because perhaps that will help me learn what I need to know.

Thanks,
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

OK, I am definately missing something

Post by irishmike2004 »

I have been through like 6 different online parsing tutorials now. I have been working non-stop from when I got up this morning to currently which is 10 pm CST. I am ready to throw in the towel and say that this is just a broken function and doesn't work in PHP. The examples I have I understand the three callback functions but I am getting no data in my arrays and I am sure that something is wrong. Could someone please upload how they would parse the XML document I have provided so I can study it.

Thanks,
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

why not try parsing a simple xml document?

your trying to jump into something too big. learning is a process,
start somewhere where you can be successfull.
then, go a little bigger. repeat as neccesary.
take small steps, 1 at a time. when something doesnt work,
then you can easily find out why, instead of looking at 100 lines
of code and just saying to yourself "it doesnt work"

your still asking someone to do it for you,
and you still have not bothered to post what your specific problem is.
w/ the way you keep continuing i dont think very many people will want to help you anymore.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: OK, I am definately missing something

Post by timvw »

irishmike2004 wrote: The examples I have I understand the three callback functions but I am getting no data in my arrays and I am sure that something is wrong
in that case you might want to post the code you already have, and then we can help you.....
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

Post by irishmike2004 »

Hi tim:

actually it would help if you could upload an example of your xml file so I can see how the actual data looks which would help my understanding of the code you showed me.

Thanks, BTW: I am trying another example right now that another forum gave me.
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

this might work if I can get the data into a variable

Post by irishmike2004 »

Someone on another forum suggested I parse my document using the following:

Code: Select all

$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, $data, $values, $indexes);
xml_parser_free($parser);
This does indeed get the parsing done, it appears to put everything into an array but I am unsure now how to pull the data I want from that array into the variable.

I think it will be something like:

Code: Select all

$currTemp = $values&#1111;$ndexes&#1111;"tmp"]&#1111;0]]&#1111;"value"];
I will let you know if that works. But please correct me if my hunch is wrong.

Thanks.
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

Post by irishmike2004 »

That worked actually so my issue is now solved.

Thanks for your help all :-)
Post Reply