#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    26
    Rep Power
    0

    Loop XML Data of Indeterminate number of elements


    I am new to PHP.

    I have looked up this issue on videos and read stuff, but there isn't exactly an example I can get to fit what I am doing. Most samples are For Loops, and I don't thing foreach works with this, at least the samples I have seen online and in books. Maybe the XML data isn't in the proper array structure? Anyway I want to loop XML data until I run out of elements/objects, but allow it to expand if my data source increases elements or something changes.

    Lets say it is Table[]->catdes

    If there are 3 instances of Table I want it to run three times, but me not have to provide a number like a for loop.

    Initial connection stuff: (it has to be massaged because the source sucks, notice there is xxx instead of http in the beginning)

    PHP Code:
    $url_value="xxx://www.ssicat.com/smart/updates.asmx/CategoryUpdate?CustomerNumber=15753&UserName=15753&Password=75035&Source=15753";

    $url_to_work_on=file_get_contents($url_value);//get the data
    $wrapping_xml=simplexml_load_string($url_to_work_on);//clean it up

    $final_data_to_parse=simplexml_load_string(html_entity_decode($wrapping_xml)); 

    I have tried
    PHP Code:
    for($i=0;$i<=100;$i++)
    {
    echo 
    $final_data_to_parse->Table[$i]->CATID."</p>"  ;
    echo 
    $final_data_to_parse->Table[$i]->CATDES."</p>" ;
    echo 
    $final_data_to_parse->Table[$i]->ATTR1."</p>" ;
    echo 
    $final_data_to_parse->Table[$i]->ATTR2."</p>" ;
    echo 
    $final_data_to_parse->Table[$i]->ATTR3."</p>" ;
    echo 
    $final_data_to_parse->Table[$i]->ATTR4."</p>" ;
    echo 
    $final_data_to_parse->Table[$i]->ATTR5."</p>" ;
    echo 
    $final_data_to_parse->Table[$i]->ATTR6."</p>" ;
    echo 
    $final_data_to_parse->Table[$i]->ATTR7."</p>" ;
    echo 
    $final_data_to_parse->Table[$i]->ATTR8."</p>" ;
    echo 
    $final_data_to_parse->Table[$i]->ATTR9."</p>" ;
    echo 
    $final_data_to_parse->Table[$i]->ATTR0."</p>" ;
    echo 
    "</p>";
    echo 
    "</p>"
    And that works, but I have to provide length


    This:

    PHP Code:

    If(is_object ($final_data_to_parse->Table[$i]->CATID))
        {
        echo 
    'Is Object';
        }
    ELSE
        {
        echo 
    'Nope';    
        } 
    runs through and determines correctly what I am after, but it gets to the end and throws a non object notice.

    Seems like a WHILE(TABLE[$i]->catdes) "has length" is just going to throw another notice.

    I also, as mentioned tried foreach, but it doesn't seem to be the right tool


    I know there has to be a simple solution and I am just too novice to know. Thanks ahead of time.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,600
    Rep Power
    595
    PHP Code:
    for($i=0;$i<count($final_data_to_parse->Table);$i++) 
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,294
    Rep Power
    9400
    You most certainly can foreach over it.
    PHP Code:
    foreach ($final_data_to_parse->Table as $table) {
        echo 
    $table->CATID."</p>"

IMN logo majestic logo threadwatch logo seochat tools logo