Results 1 to 12 of 12

Thread: Reading XML

Threaded View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2007
    Location
    St Charles, Mo
    Posts
    171

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width