SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2002
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XML Attribute Parse Issue

    I'm having a xml parse issue where my code isn't looping through all the elements, just the first one.

    Here is the xml code:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <weblogUpdates version="2" updated="Thu, 04 Sep 2008 05:21:00 GMT" count="2301253">
    
        <weblog name="Una Cuestin de Propsito" url="http://unacuestiondeproposito.blogspot.com/" when="2842" />
    
        <weblog name="GMAT Math" url="http://www.scorechase.com/gmat" when="2843" />
    </weblogUpdates>
    The php code:
    PHP Code:

    $xml 
    simplexml_load_file("changes.xml");

    foreach(
    $xml->weblog[0]->attributes() as $a => $b)
    {
        echo 
    $a.'="'.$b."\"</br>";

    This is what is output:

    name="Una Cuestin de Propsito"
    url="http://unacuestiondeproposito.blogspot.com/"
    when="2842"

    I would obviously like to loop through all of them. Any idea what i'm missing?

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Haven't used simpleXML before but this is a wild stab in the dark:
    PHP Code:
    $xml simplexml_load_file("changes.xml");
    foreach(
    $xml->weblog as $weblog){
        echo 
    '<hr />';
        foreach(
    $weblog->attributes() as $a => $b){
            echo 
    $a.'="'.$b."\"</br>";
        }

    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You are only iterating through one element, try this...

    PHP Code:
    <?php

    $xml 
    simplexml_load_file("changes.xml");

    foreach (
    $xml->weblog as $webLog )
    {
        foreach(
    $webLog->attributes() as $a => $b)
        {
            echo 
    $a.'="'.$b."\"</br>";
        } 
    }

    ?>
    SilverB.

  4. #4
    SitePoint Zealot
    Join Date
    Jan 2002
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    d'oh! Thanks guys. Works perfect.


Bookmarks

Posting Permissions

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