View Full Version : Reading xml elements into php array

02-04-2010, 09:07 PM

I have an xml file with elements like this:

0.042 0.0513 0.062 ...

I want to make the above look like this:

<speed>0.042 </speed>

So that I can read each value into php with something like

$f = "file.xml";
$xml = simplexml_load_file($f) or die ("Unable to load XML file!");
$val1 = $xml->speed[1];

Can anyone help me?

Many thanks


02-04-2010, 09:20 PM
I'm a little confused. Where exactly does php come into play here? What I see, is a manual edit of an xml file thats necessary in order to load what you have. You can certainly open what you have now, fetch the speed element and split it up into individual speed elements by replacing the current speed. For that, I'd suspect you'll need to use the DOM instead of simplexml. I myself have used simplexml like twice, just to answer questions here.

02-05-2010, 06:04 PM
Basically you would need to read the original line, trim() (http://us.php.net/trim) it, explode() (http://us.php.net/explode) by space (for more complicated case you would preg_split() (http://us.php.net/preg_split) it but here explode() should be enough I think) and write to a new XML file using SimpleXML you are using. This is if I have understood the problem correctly.