Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2010
    Thanked 0 Times in 0 Posts

    Reading xml elements into php array


    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


  2. #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Saskatoon, Saskatchewan
    Thanked 2,668 Times in 2,637 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  3. #3
    Regular Coder
    Join Date
    Mar 2006
    Thanked 37 Times in 37 Posts
    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.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts