|
-
Jan 22nd, 2010, 11:26 PM
#1
Thread Starter
Addicted Member
Reading XML
I am just learning some php and I am having an issue. I am trying to read through an xml doc and create web links. This is the code I am trying to use.
Code:
<?php
$file = "includes/links.xml";
$xml = simplexml_load_file($file) or die ("Unable to load XML file!");
echo "loaded";
if($xml)
{
foreach($xml->link->links as $link)
{
$url = $link->linkurl;
$sitename = $link->sitename;
echo '<a href="'.$url.'">'.$sitename.'</a><br />';
}
}
?>
And here is what the xml will look like.
Code:
<links>
<link>
<category>News</category>
<linkurl>http://www.cnn.com</linkurl>
<sitename>CNN</sitename>
</link>
<link>
<category>Tech</category>
<linkurl>http://www.google.com</linkurl>
<sitename>Google</sitename>
</link>
</links>
Ideally I would like to figure out how to read the category and then write the urls that fit the category. Right now it is loading but I am not getting any results.. I am quite sure the syntax is off so I am asking for some help.
Thanks
Last edited by Spiffyguy; Jan 22nd, 2010 at 11:31 PM.
-
Jan 23rd, 2010, 11:40 AM
#2
Re: Reading XML
When you use simplexml_load_file(), the variable that you load it into ($xml, in your case) becomes synonymous with the root element of your XML. So in your foreach loop, what you want is the element <link> which is under root element <links> - "$xml->link->links" is not specifying this. Change to "$xml->link".
Make sense?
-
Jan 23rd, 2010, 12:03 PM
#3
Thread Starter
Addicted Member
Re: Reading XML
yeah that makes total sense. So using this code.
Code:
<?php
$file = "includes/links.xml";
$xml = simplexml_load_file($file) or die ("Unable to load XML file!");
if($xml)
echo "loaded";
{
foreach($xml->links as $link)
{
$url = $link->linkurl;
$sitename = $link->sitename;
echo '<a href="'.$url.'">'.$sitename.'</a><br />';
}
}
?>
It echoes that it has loaded the document, which is good for me lol, but it does not list the links. so int he foreach statement is this statement right? $url = $link->linkurl; What I am getting it is should set the variable $url to the contents of link->linkurl which should be the website.
-
Jan 23rd, 2010, 12:23 PM
#4
Thread Starter
Addicted Member
Re: Reading XML
ahh figured it out. I had links instead of link. ACK. Thanks for the help. Gets me one step closer.
-
Jan 23rd, 2010, 12:53 PM
#5
Thread Starter
Addicted Member
Re: Reading XML
Just one more question. If I want to sort the results and create the html based on the category, so all the tech links would be listed, then all the whatever. Would it be better form to pull the xml into an array and then sort? or try to xpath through the file?
Thanks
-
Jan 23rd, 2010, 03:15 PM
#6
Re: Reading XML
I'd probably just go with XPath...
Code:
<?php
$order = array('Tech','News');
$xml = simplexml_load_file("links.xml") or die ("Unable to load XML file!");
foreach($order as $o){
$result = $xml->xpath("link[category='$o']");
foreach($result as $link) {
echo '<a href="'.$link->linkurl.'">'.$link->sitename.'</a><br />';
}
}
?>
Don't really know if it's "better," but it seems like less work to me.
-
Jan 23rd, 2010, 05:40 PM
#7
Thread Starter
Addicted Member
Re: Reading XML
Well I am trying to sort the results based on category and then display them using HTML. So some HTML then list all the tech then close out that category. Then move on to the next category, etc like that. I wasn't sure if you could use Xpath or if you would need to use an If statement on the results.
-
Jan 23rd, 2010, 08:43 PM
#8
Re: Reading XML
.. as far as I can tell, that's exactly what Samba's code does. so I'm not sure what you're asking.
you have an array of the categories you want to display, and then you loop through them and create an XPath to that category. then, you loop through the results of the XPath. then it repeats. if you need to "close off" a category, it would be simple. a category starts in the first foreach() loop, and ends after the nested foreach() loop. if you were using <ul> as an opening, and </ul> to close:
PHP Code:
<?php //main foreach foreach( .... ){
echo '<ul>';
//nested foreach foreach( .... ){
echo '<li>......</li>';
}
echo '</ul>'; } ?>
as always, I'd advise against using echo to print HTML; it makes things much harder to read and sometimes a lot harder to maintain. PHP is an embedded language, and thus can be treated as if it were a part of the HTML itself:
PHP Code:
<html> <head> <title><?php echo $title; ?></title> <head> <body> <h1><?php echo $title; ?></h1>
<h2>categories</h2> <?php foreach( .... ): ?> <h3><?php echo $category; ?></h3> <ul> <?php foreach( .... ): ?> <li><?php echo $link; ?></li> <?php endforeach; ?> </ul> <hr /> <?php endforeach; ?> </body> </html>
hope that makes sense! oh, and I'm using the more verbose form of opening/closing the foreach statements while embedded because at times it can also be easier to read. with not very much code, it may not be needed, or it may not be your preference to do so!
-
Jan 23rd, 2010, 09:24 PM
#9
Thread Starter
Addicted Member
Re: Reading XML
basically that is what I am trying to do. display the link by category using <ul>. I like the idea of snagging the category as a variable, saves having to hard code it. I will try to play with it a bit more. certainly appreciate the help. if this was VB I would know how to do it lol.
-
Jan 23rd, 2010, 09:29 PM
#10
Re: Reading XML
I'd advise against using echo to print HTML; it makes things much harder to read and sometimes a lot harder to maintain.
I've seen a few members here concur with that, I've thought about it while writing code, and personally I don't agree that it always makes things easier to read or to maintain. Just curious, is there any real technical reason not to echo it, or just a preference?
-
Jan 23rd, 2010, 11:25 PM
#11
Re: Reading XML
PHP ignores anything outside of the PHP tags; it would be faster for PHP to be able to ignore large blocks of HTML than to output it all, but if you're echoing everything then PHP has to process it.
I have read some benchmarks saying that it's better performance-wise to echo out a large block of dynamic text (including HTML) rather than opening and closing the PHP tags continuously (assuming that there is very little text in between every opening), but I'm not sure of the validity of this claim. the assumption I get from it is that:
PHP Code:
<?php echo $var1; ?>, <?php echo $var2; ?>, <?php echo $var3; ?>
is slower than:
PHP Code:
<?php echo $var1 . ', ' . $var2 . ', ' . $var3; ?>
which makes sense if you think about it, because of whatever possible overhead is incurred when jumping in and out of "PHP mode." of course, my example didn't include any HTML, but my general thoughts are that I will usually always embed my PHP code within my HTML to promote readability/maintainability, unless (as you mentioned) it's a case where it would not help out.
as far as I know, the performance hit that I had read about in the article is not very serious, but I've not done any looking into it myself.
really, I just hate having to escape anything that I don't need to. when I embed PHP into my HTML, I never need to; if I echo out all of my HTML, then I would always need to. this would be a pain to maintain.
-
Jan 24th, 2010, 01:27 AM
#12
Re: Reading XML
I suppose I think there're appropriate scenarios for each case; if you've got a lot of HTML to output, of course don't use echo, but when it's something like your [of course a bit arbitrary] example (I mean simplify that down to echo "$var1, $var2, $var3" and it's even less of a contest), it's cumbersome and overly-verbose to keep going in and out of PHP.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|