Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-04-2010, 08:07 PM   PM User | #1
lilya
New to the CF scene

 
Join Date: Feb 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
lilya is an unknown quantity at this point
Reading xml elements into php array

Hello.

I have an xml file with elements like this:

<speed>
0.042 0.0513 0.062 ...
</speed>


I want to make the above look like this:

<speed>0.042 </speed>
<speed>0.0513</speed>
<speed>0.062</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

Lily
lilya is offline   Reply With Quote
Old 02-04-2010, 08:20 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 02-05-2010, 05:04 PM   PM User | #3
SKDevelopment
Regular Coder

 
Join Date: Mar 2006
Posts: 238
Thanks: 3
Thanked 37 Times in 37 Posts
SKDevelopment has a little shameless behaviour in the past
Basically you would need to read the original line, trim() it, explode() by space (for more complicated case you would 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.
__________________
PHP Programmer
SKDevelopment is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:17 AM.


Advertisement
Log in to turn off these ads.