ladinek111 Posted April 25, 2010 Share Posted April 25, 2010 Hello Here is my problem: I am reading data from MySQL DB like this: <polygons> <polygon name="Polygon11" address="London" latlng="49.837 17.992,49.828 18.055,49.840 18.063,49.837 18.024" /> <polygon name="Polygon21" address="Berlin" latlng="49.828 18.023,49.830 18.058,49.842 18.047,49.839 18.027,49.828 18.023" /> I need data like this: <polygons> - <polygon name="Polygon11" address="London" > <point lat="49.837" lng="17.992" /> <point lat="49.828 " lng="18.055" /> <point lat="49.840 " lng="18.063" /> <point lat="49.837" lng="17.992" /> </polygon> <polygon name="Polygon21" address="Berlin" > <point lat="49.837" lng="17.992" /> <point lat="49.828 " lng="18.055" /> <point lat="49.840 " lng="18.063" /> <point lat="49.837" lng="17.992" /> </polygon> </polygons> I try something with explode function, but it isn't work. Please Help. thank's Link to comment https://forums.phpfreaks.com/topic/199706-parsing-xml-by-php/ Share on other sites More sharing options...
de.monkeyz Posted April 25, 2010 Share Posted April 25, 2010 I would recommend using an xml parser class like SimpleXML to read the string data and grab the values. //Assuming $data is the string containing the xml from the database $xml = simplexml_load_string($data); //Output start of xml echo '<polygons>'; //Loop through the polygons foreach($xml->polygon as $poly) { //Grab attributes $name = $poly->attributes()->name; $address = $poly->attributes()->address; $loc_string = $poly->attributes()->latlng; //Extract points $locations = explode(',', $loc_string); //Output node echo '<polygon name="' , $name , '" address="' , $address , '">'; //Loop through the points foreach($locations as $loc) { //Extract latitude and longitude $lat_lon = explode(' ', $loc); echo '<point lat="' , $lat_lon[0] , '" lng="',$lat_lon[1] , '" />'; } echo '</polygon>'; } //Output end of xml echo '</polygons>'; That would output the xml you're looking for. Link to comment https://forums.phpfreaks.com/topic/199706-parsing-xml-by-php/#findComment-1048210 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.