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
$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.
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.
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:
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