Extract value from XML using PHP

Discussion in 'PHP' started by bigtime, Jan 7, 2012.

  1. #1
    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:

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

    
    $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);
    
    PHP:
    Please help put an end to my two days of misery trying to figure this out :)

    Thanks,

    Tim
     
    Last edited: Jan 7, 2012
    bigtime, Jan 7, 2012 IP
  2. modz

    modz Well-Known Member

    Messages:
    95
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    103
    #2
    
    $string = "
    <Course>
      <LearnerID value=\"tim@mail.net\"/>
      <Result>
        <CoreData>
          <Status value=\"passed\"/>
          <Location value=\"1\"/>
            </CoreData>
      </Result>
    </Course>";
    preg_match ('!<Status value="(.+)"/>!', $string, $result);
    echo $result[1];
    
    PHP:
    Make the adjustments by yourself. If you can't, ask us for help. The backslashes are for escaping, don't worry about them. Just assign the value of the DB field to $string.
    Why do you store tags in the database? It's senseless.
     
    modz, Jan 8, 2012 IP
  3. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #3
    Seeing as how it's not a valid XML string, you won't be able to parse it as such. modz solution should work though.
     
    Alex Roxon, Jan 8, 2012 IP
  4. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #4
    Because the key has invalid stdClass variable name, you will need to convert that sub element to an array:

    
    $xml = simplexml_load_string($Filedata);
    $item = (array) $xml->Result->CoreData->Status;
    echo $item['@attributes']['value'];
    
    PHP:
     
    ThePHPMaster, Jan 8, 2012 IP
  5. bigtime

    bigtime Peon

    Messages:
    226
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for all the replies. I appreciate the time all of you took to reply.

    .

    I agree. It's a program I have no control over that is inserting it. I'm stuck with having to write an interface to extract the values.

    I ended up using ThePHPMaster's solution and it's working exactly as I need it to. I didn't try modz solution but it looks great as well.

    Thanks!

    Tim
     
    bigtime, Jan 8, 2012 IP