SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Location
    Houston, TX
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Extract value from XML using PHP

    I want to get the value of "passed" under Course/CoreData/Status and assign it to a PHP variable. Here's my data stored in a mysql database field:

    PHP Code:
    <Course>
      <
    LearnerID value="tim@mail.net"/>
      <
    Result>
        <
    CoreData>
          <
    Status value="passed"/>
          <
    Location value="1"/>
            </
    CoreData>
      </
    Result>
    </
    Course
    I've tried the following code as a start but to no avail:

    PHP Code:
    $sql "SELECT * FROM exam";
    $q     mysql_query($sql) or die(mysql_error());

    while(
    $row=mysql_fetch_array($q)){
        
    $Filedata=$row["Filedata"];
    }

    $xml simplexml_load_string($Filedata); 
    Please help put an end to my two days of misery trying to figure this out

    Thanks,

    Tim

  2. #2
    SitePoint Member
    Join Date
    Apr 2010
    Location
    Near Portsmouth UK, UK post code district PO12
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <?php
    // Suggest the XML needs to be written in long form (not using attributes like <Status value="passed"/>).
    // And the XML needs to be retrieved as a string:

    $response = <<<XML
    <Result>
    <LearnerID>tim@mail.net</LearnerID>
    <CoreData>
    <Status>passed</Status>
    <Location>1</Location>
    </CoreData>
    </Result>
    XML;

    $xml = simplexml_load_string($response);

    // print_r($xml);

    foreach ($xml->CoreData as $coredata) {
    echo 'Status: '.$coredata->Status.'; Location: '. $coredata->Location;
    }

    ?>

  3. #3
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $Filedata '<Course> 
      <LearnerID value="tim@mail.net"/> 
      <Result> 
        <CoreData> 
          <Status value="passed"/> 
          <Location value="1"/> 
            </CoreData> 
      </Result> 
    </Course>'
    ;

    $xml simplexml_load_string($Filedata);
    $status = (string) $xml->Result->CoreData->Status['value'];

    echo 
    $status// passed 
    See http://php.net/simplexml.examples-basic
    Salathe
    Software Developer and PHP Manual Author.


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
  •