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

    Join Date
    Jul 2013
    Posts
    20
    Rep Power
    0

    XML Element Name As variable


    I have this code

    PHP Code:

    $url_to_work_on
    =file_get_contents($url_value);//get the data
    $wrapping_xml=simplexml_load_string($url_to_work_on);//clean it up
    $wrapping_xml=str_replace("&#x1A","",$wrapping_xml);//clean out goofy characters
    $final_data_to_parse=simplexml_load_string($wrapping_xml);

    $child=$final_data_to_parse->children();//This gets the children of the main data (i.e, this retruns "TABLE" since its NewDataSet>Table
    $parenttag=$child->getName();//holds the root XML tag name




    for($i=0;$i<count($final_data_to_parse->$parenttag);$i++)
        {
        foreach(
    $child->children() as $child2)//This goes through TABLE and gets the sub names
            
    {
            
    $value=$child2->getName();
            
    //echo "$value<br />\n";
            
    Echo $final_data_to_parse->Table[$i]->$value."<br />\n";
            
    ///echo $child2->getName() . "<br />";    
            
    }
        } 
    That code works. But I don't want to use "Table[$i]", I want to sue $parenttag[$i] and the value of $parenttag is in fact "Table"



    It doesn't work if one does
    PHP Code:
    Echo $final_data_to_parse->$parenttag[$i]->$value."<br />\n"
    What am I missing?
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    PHP Code:
    $final_data_to_parse->{$parenttag}[$i
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    20
    Rep Power
    0
    Perfect! That Worked!

IMN logo majestic logo threadwatch logo seochat tools logo