-1

I'm exporting some data from a kml file to mysql, there's an xml chunk <multigeometry> that I would like to save in the database as raw xml (because it's too complex to break down), my plan was to use json_encode and then convert the json back to xml when reading the mysql table but this task is proving to be very complicated,

Is it possible to just get the xml from the stdclass somehow? Or is there a better way of getting this saved in the database and converting back to XML?

$xml = simplexml_load_file("countries.kml")
foreach($xml->children() as $nodes){
    foreach($nodes->children() as $n => $data){
        mysql_query("insert into tbl_countries (varname,ISOA2,multigeometry) values ('".$data->name."','".substr($data->description,7,2)."','".json_encode($data->MultiGeometry)."')") or die(mysql_error());
          }
}
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
Jim SMith
  • 212
  • 3
  • 13
  • http://stackoverflow.com/questions/4906073/a-simple-program-to-crud-node-and-node-values-of-xml-file – Gordon Oct 26 '12 at 08:56
  • 1
    How was I supposed to find that? It didn't come up in related – Jim SMith Oct 26 '12 at 09:03
  • well, there is also the easily accessible http://php.net/manual/en/simplexml.examples-basic.php and about every second question dealing with SimpleXml showing how to get the outerXml. – Gordon Oct 26 '12 at 09:06
  • [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained and the [deprecation process](http://j.mp/Rj2iVR) has begun on it. See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – tereško Nov 02 '12 at 21:03

1 Answers1

0

You should be able to get the xml part recreated from any SimpleXMLElement with its asXML method.

Just replace the json_encode($data->MultiGeometry) part with $data->MultiGeometry->asXML().

Note: The obligatory please don't use mysql_* functions anymore, they are deprecated, see the choosing mysl api page.

complex857
  • 20,425
  • 6
  • 51
  • 54