Help - Search - Members - Calendar
Full Version: XML in PHP4
Invision Power Services > Community Forums > Community Web Design and Coding
Michael Boutros
Hello,

I am trying to parse an XML file for 3 of it's fields, but I have no idea how to. I know in PHP5 we have SimpleXML, but what can I use in PHP4? Below is an example of one of the items in the XML file. The fields in bold are the ones I need.

QUOTE
<item>
<title>Gemineye - Break Neck Radio (The Afro-Takeover)</title>

<link>
http://podcast.digital-djs.com/imix/default.asp?id=11070
</link>

<description>
01. (Intro) Spencer Doran - First of All 02. J. Dilla - Thunder 03. Blu - Soul Amazing (Soul Provider Remix) 04. Session - Freestyle 05. Jahi feat. Dwele - On My Own 06. Q-Tip feat. Andre 3000 - That's Sexy 07. (Break #1) Cyrus Tha Great - Island Music 08. Jay-Z - 99 Problems (DL's Firefly Remix) 09...
</description>

<pubDate>Mon, 31 Jul 2006 10:27:33 EDT</pubDate>
<source url="http://feeds.feedburner.com/DDJiMix">Digital-DJs.com (Podcasts)</source>
<bd:channelLink>http://www.digital-djs.com</bd:channelLink>
<enclosure url="http://broadcast.underground-fusion.com:8080/iMixes/Gemineye/Break%20Neck%20Radio%20(The%20Afro-Takeover).mp3" type="audio/mpeg"/>
<media:content url="http://broadcast.underground-fusion.com:8080/iMixes/Gemineye/Break%20Neck%20Radio%20(The%20Afro-Takeover).mp3" type="audio/mpeg"/>
</item>

<item>


Thanks,
Michael Boutros
Brendon Koz
It's always beneficial these days to go with PHP5, unless you have clients that require PHP4.

http://developer.yahoo.com/download/download.html

You can download the developer's kit and check out the PHP code. Example 1 and 2 was (sloppy) code from Rasmus Lerdorf on using PHP4 to parse the XML file that Yahoo's SEarch API returned.
Michael Boutros
Thanks for that, I'll check it out. I wanted to go with PHP5, but unfortunately the client only has PHP4.
Rikki
I believe PEAR has classes for XML original.gif
princetontiger
I've read PHP5 has awesome XML support
Michael Boutros
PHP5 has the best XML support ever using SimpleXML, but PHP4 is another story.

The Pear XML package is not working at all for me, and neither is the Yahoo one unfortunately. I am willing to pay if someone can help me get those 3 things from the XML that I originally posted.

Thanks,
Michael Boutros
Wombat
This PHP library for parsing XML allows you to parse data-oriented XML (think RSS, Atom et. al.) into nested arrays for easy access.

Should be pretty simple with the help of that library.
Michael Boutros
QUOTE(Wombat @ Aug 3 2006, 01:18 PM) *
This PHP library for parsing XML allows you to parse data-oriented XML (think RSS, Atom et. al.) into nested arrays for easy access.

Should be pretty simple with the help of that library.


Thank you very much, that script is exactly what I need, and according to the online version it will work. However, when I try to run it, I get an error. This is my code:

CODE
<?php
include("parser.php");

$url = "core/file.xml";
$xml = file_get_contents($url);

$data = XML_unserialize($xml);

new dBug($data);
?>


(the dBug class is included elsewhere)

The error that is thrown is:
CODE
Notice: Only variable references should be returned by reference in /home/michael/public_html/dev/work/search/core/parser.php on line 73
[empty string]


Line 73 is:
CODE
return xml_parse(&$this->parser, &$data, true) ? $this->document : NULL;


Thanks a lot,
Michael Boutros
Starnox
tried removing the &'s?
Brendon Koz
Just out of curiosity, the XML is properly constructed, right? Proper content-type header and everything?

If you have a link to the exact source, or a similar one I can try to get something working later tonight since the movie I was going to see tonight isn't out until next week (darn advertisements always make me think I'm a week ahead of myself)...that is, if you don't get it yourself first. biggrin.gif
Michael Boutros
Thanks maliktye, but I got it to work thanks to that class. I ended up just returning $this->document, but I did learn about using the ? operator, which is really amazing to me tongue.gif Is it advised not to use the ? operator? It seems like it could cut down on a lot of code. Ex:

CODE
$errors = (!isset($form_var)) ? "There was an error." : NULL


Would usually be:

CODE
if (!isset($form_var)){
$errors = "There was an error.";
} else {
$errors = NULL;
}
Rikki
The ? operator is fine to use, it's called the Ternary operator.
Michael Boutros
So using the ternary operator wouldn't be considered unreadable code?
Brendon Koz
It depends on the developer. Some people don't like it because it's harder to determine work-flow. However, I would have to imagine that things like that would be found on the Daily WTF site...like when someone has a 700 character long line using the ternary operator...or perhaps 5 of them in one.

It's usually fine to use (imo) so long as it's kept short and simple, otherwise break it up for readability purposes. original.gif
Antony
The ternary operator can be used to make code more readable in my opinion:

CODE
echo 'Hello '.($member ? $member['name'] : 'Guest').'.';
Brendon Koz
That's an opinion. It's not quite as english-like in readability. For instance, using your example, take the following counter example:
CODE
if($member){
    echo 'Hello ', $member['name'], '.';
}else{
    echo 'Hello Guest.';
}


I can personally read that code as if it were pseudocode: "If a member, echo to the standard output "hello _membername_"; otherwise output "Hello Guest".

Yours may make the code itself neater (since we're not breaking/changing indentation at all) but for readability in the understanding of the code, it's a bit more difficult. Now if you assigned $member['name'] or 'Guest' to a variable first, then I would agree with you. Combining an echo and an assignment -- good example for me to contradict, regardless of simplicity. But, again -- some developers prefer it one way, others another. It's personal preference depending on who's looking at the code... Either way, this example is more trivial than anything, it's when things get more formulaic that it truly matters.

...anyway, I'll shutup. biggrin.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.