andrew_php Posted June 26, 2008 Share Posted June 26, 2008 Hello All! Quick question, I have a table in MySQL I want to export as XML, which I have been able to fine with $xml_output, however I have one field which is semi-colon deliminated text that I would like to seperate into individual elements. eg. table_x ************************************************* * Name * Color * * * * ************************************************* * Billy * Blue * ************************************************* * Sally * Green;Blue * ************************************************* * Pedro * Red;Green;Blue;Yellow * ************************************************* I would like the output to be: <root> <name>Billy</name> <favourite_colors> <color>Blue</color> </favourite_colors> <name>Sally</name> <favourite_colors> <color>Green</color> <color>Blue</color> </favourite_colors> <name>Pedro</name> <favourite_colors> <color>Red</color> <color>Green</color> <color>Blue</color> <color>Yellow</color> </favourite_colors> </root> I am somewhat still a noob, so apologies in advance if I have not expressed this request as simply as possible. Any help would be greatly appreciated. Kind Regards, Andrew Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted June 26, 2008 Share Posted June 26, 2008 I don't use xml all that much, but I'd say when you have the color variable ready(with all the colors separated by , do a $temp = explode(";", $colors) and then step through each variable, printing a <color> tag for each. Remember to ignore the last one though, since it will be blank(since there's nothing on the right side of your last semicolon) Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted June 26, 2008 Share Posted June 26, 2008 you may want to check SimpleXML Functions. This is existing already for PHP version 5.? Quote Link to comment Share on other sites More sharing options...
Barand Posted June 26, 2008 Share Posted June 26, 2008 try <?php $data = array ( array('Billy', 'Green'), array('Sally','Green;Blue'), array('Pedro' , 'Red;Green;Blue;Yellow') ); $str = "<root>\n"; foreach ($data as $person) { $str .= "<person>\n"; $str .= "<name>{$person[0]}</name>\n"; $str .= "<favorite_colors>\n"; $colors = explode(';', $person[1]); foreach ($colors as $fc) { $str .= "<color>$fc</color>\n"; } $str .= "</favorite_colors>\n"; $str .= "</person>\n"; } $str .= "</root>\n"; echo $str; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.