PDA

View Full Version : [SOLVED] Php SimpleXMLElement()



Dr Small
November 15th, 2008, 02:31 AM
Greetings, I am messing around with extracting different stuff from both Atom and RSS feeds, and have got it mostly figured out, but have this one problem when attempting with Atom.

I'll give you some examples of what I am trying to do, and then maybe you can help me.

I have this XML:

<entry>
<author>
<name>K.Mandla</name>
<uri>http://kmandla.wordpress.com/</uri>
</author>
<title type="html"><![CDATA[Internet Explorer balks at Firefox download link]]></title>
<link rel="alternate" type="text/html" href="http://kmandla.wordpress.com/2008/11/13/internet-explorer-balks-at-firefox-download-link/" />
<id>http://kmandla.wordpress.com/?p=2041</id>
<updated>2008-11-13T11:15:25Z</updated>
<published>2008-11-13T11:15:25Z</published>
<category scheme="http://kmandla.wordpress.com" term="Arch Linux" /><category scheme="http://kmandla.wordpress.com" term="Linux" /><category scheme="http://kmandla.wordpress.com" term="Ubuntu" /> <summary type="html"><![CDATA[This is amusing to me; I’m sure there’s a very good, very rational reason for this issue and I’m probably just not sharp enough to catch it.
But on a clean installation of Windows XP (don’t ask; it’s a long story), Internet Explorer 6 claims there’s an error on the GetFirefox.com download page, and refuses to [...]]]></summary>
<content type="html" xml:base="http://kmandla.wordpress.com/2008/11/13/internet-explorer-balks-at-firefox-download-link/"><![CDATA[<div class='snap_preview'><br /><p>This is amusing to me; I’m sure there’s a very good, very rational reason for this issue and I’m probably just not sharp enough to catch it.</p>

<p>But on a clean installation of Windows XP (don’t ask; it’s a long story), Internet Explorer 6 claims there’s an error on the GetFirefox.com download page, and refuses to allow a click on the download button.</p>
<p>A screenshot is the best I can do here, since I don’t really have any other way of proving it won’t work. I restarted IE and tried twice, but both times I get a supposed “error report” for that page (see the bottom left of the IE status bar) and double-clicking that gives the error report on the upper right. The pointer does not turn into a “hand” anywhere near the giant green download button.</p>
<p>I’ve highlighted and put a red oval around the offending “line 190″ from the page source.</p>

<p align="center"><a href="http://xs.to/xs.php?h=xs233&amp;d=08464&amp;f=line-190-943.jpg" target="_blank"><img src="http://xs233.xs.to/xs233/08464/line-190-943.jpg.xs.jpg" /></a></p>
<p>If there’s an error in that line, I don’t know enough Web code to see it. Somebody smarter please clue me in.</p>
<p>For the record, that’s a fresh SP2 installation from a legitimate disc, and I haven’t done any upgrades except for the first round, as was suggested by the update notifier … or whatever Microsoft is calling it these days. It would be ironic if the first thing that little yellow shield did was hard-code IE to ignore the GetFirefox page. Of course, I wouldn’t put it past them.</p>

<p>I could still follow the links to the interior of the site and download it from the list of language selections. And yes, the same page with the same code at the same line works fine in Firefox, of course.</p>
<p>I will admit it gave me a laugh for a second though. </p>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kmandla.wordpress.com/2041/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kmandla.wordpress.com/2041/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kmandla.wordpress.com/2041/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kmandla.wordpress.com/2041/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kmandla.wordpress.com/2041/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kmandla.wordpress.com/2041/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kmandla.wordpress.com/2041/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kmandla.wordpress.com/2041/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kmandla.wordpress.com/2041/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kmandla.wordpress.com/2041/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kmandla.wordpress.com&blog=353512&post=2041&subd=kmandla&ref=&feed=1" /></div>]]></content>
<link rel="replies" type="text/html" href="http://kmandla.wordpress.com/2008/11/13/internet-explorer-balks-at-firefox-download-link/#comments" thr:count="11"/>
<link rel="replies" type="application/atom+xml" href="http://kmandla.wordpress.com/2008/11/13/internet-explorer-balks-at-firefox-download-link/feed/atom/" thr:count="11"/>
<thr:total>11</thr:total>
</entry>

I want to extract the part from the XML which is in bold. If I try the following, I extract the first <link>, but since Atom names it's objects the same, I am finding it difficult to get the comments link.

$link = (string)$item->link['href'];

Of course, that is just taking the first <link> and echoing it on the page. Is there any way to do what I want to do?

Dr Small

drubin
November 15th, 2008, 11:14 AM
RSS readers are very specialized xml readers much the same with html. I would recommend using a RSS/Atom reader for this. :)

Here is the one I would use.
http://magpierss.sourceforge.net/
http://www.scriptol.com/rss/rss-reader.php

I will take a look at your problem with the xml reader though and get back to you.

drubin
November 15th, 2008, 11:19 AM
Greetings, I am messing around with extracting different stuff from both Atom and RSS feeds, and have got it mostly figured out, but have this one problem when attempting with Atom.

I'll give you some examples of what I am trying to do, and then maybe you can help me.

I have this XML:

<link rel="replies" type="text/html" href="http://kmandla.wordpress.com/2008/11/13/internet-explorer-balks-at-firefox-download-link/#comments" thr:count="11"/>
<link rel="replies" type="application/atom+xml" href="http://kmandla.wordpress.com/2008/11/13/internet-explorer-balks-at-firefox-download-link/feed/atom/" thr:count="11"/>
<thr:total>11</thr:total>
</entry>


$link = (string)$item->link['href'];

Try this.
http://www.php.net/simplexml-element-attributes#73811

You bassically need to get an array of links; or maybe $item->next();
If these don't work I will take a closer look later for you.

Dr Small
November 16th, 2008, 12:10 AM
Try this.
http://www.php.net/simplexml-element-attributes#73811

You bassically need to get an array of links; or maybe $item->next();
If these don't work I will take a closer look later for you.
Since I was already in a foreach() loop, I just went ahead and changed it to:

$link = (string)$item->link[1]['href'];

And it worked perfectly, as expected. It's generally the simple things that nag and trip me up for awhile. Thanks for responding and sending me to those links, drubin. Problem Resolved :)

Basically, I am writing my own RSS/Atom reader and working on it from there. I guess I am reinventing the wheel, by not using magpierss, but hey, I like to learn the inner workings of the engine to see how the wheel spins :D

drubin
November 16th, 2008, 12:30 AM
Since I was already in a foreach() loop, I just went ahead and changed it to:

$link = (string)$item->link[1]['href'];

And it worked perfectly, as expected. It's generally the simple things that nag and trip me up for awhile. Thanks for responding and sending me to those links, drubin. Problem Resolved :)
Glad it worked and it is always the simple things that mess us up :)



Basically, I am writing my own RSS/Atom reader and working on it from there. I guess I am reinventing the wheel, by not using magpierss, but hey, I like to learn the inner workings of the engine to see how the wheel spins :D
"I would re-invent the wheel 100 times over if it meant I would learn some thing from it." (Not my quote but very appropriate)