Aaargh - XML parsing!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sandman

    Aaargh - XML parsing!

    Hello!

    I've looked and searched and tried to find information about how to solbve this
    problem, but I can't. I'm beginning to think that I should start from scratch
    to do it right.

    I am trying to parse the data from the weather channel in PHP. A slice of the
    data looks like this:

    <weather ver="2.0">
    <cc>
    <lsup>3/4/05 8:50 AM Local Time</lsup>
    <obst>Vastera s, Sweden</obst>
    <tmp>-7</tmp>
    <flik>-12</flik>
    <t>Mostly Cloudy</t>
    <icon>28</icon>
    <bar>
    <r>1,010.8</r>
    <d>steady</d>
    </bar>
    <wind>
    <s>10</s>
    <gust>N/A</gust>
    <d>180</d>
    <t>S</t>
    </wind>
    <hmid>83</hmid>
    <vis>10.0</vis>
    <uv>
    <i>0</i>
    <t>Low</t>
    </uv>
    <dewp>-9</dewp>
    <moon>
    <icon>22</icon>
    <t>Waning Half, Last Quarter</t>
    </moon>
    </cc>
    </weather>

    Now, <cc> is "Current Conditions" and that's the part I would want to fetch. IN
    order to read this and handle it in PHP I would very much like to read all the
    data into an array. In other words, I would like the above in this format:

    Array
    (
    [weather] => Array
    (
    [ver] => 2.0
    [cc] => Array
    (
    [lsup] => >3/4/05 8:50 AM Local Time
    [obst] => Vasteras, Sweden
    [tmp] => -7
    [flik] => -12
    [t] => Mostly Cloudy
    [icon] => 28
    [bar] => Array
    (
    [r] => 1,010.8
    [d] => steady
    )
    [wind] => Array
    (
    [s] => 10
    [gust] => N/A
    [d] => 180
    [t] => S
    )
    [hmid] => 83
    [vis] => 10
    [uv] => Array
    (
    [i] => 0
    [t] => Low
    )
    [dewp] => -9
    [moon] => Array
    (
    [icon] => 22
    [t] => Waning Half, Last Quarter
    )
    )
    )
    )


    Which obviously would be the same as defining it like this:

    $data = array(
    "weather" => array(
    "ver" => "2.0",
    "cc" => array(
    "lsup" => ">3/4/05 8:50 AM Local Time",
    "obst" => "Vasteras, Sweden",
    "tmp" => "-7",
    "flik" => "-12",
    "t" => "Mostly Cloudy",
    "icon" => 28,
    "bar" => array(
    "r" => "1,010.8",
    "d" => "steady"
    ),
    "wind" => array(
    "s" => 10,
    "gust" => "N/A",
    "d" => 180,
    "t" => "S"
    ),
    "hmid" => 83,
    "vis" => 10.0,
    "uv" => array(
    "i" => 0,
    "t" => "Low"
    ),
    "dewp" => "-9",
    "moon" => array(
    "icon" => 22,
    "t" => "Waning Half, Last Quarter"
    )
    )
    )
    );


    Is there ANY way to get the XML data in to this format in a easy and painless
    way? I would very much like to just have it this way:

    $data = xml_to_array($x mldata);

    Thanks for any help!

    --
    Sandman[.net]
  • ZeldorBlat

    #2
    Re: Aaargh - XML parsing!

    PHP has some built in XML functionality, or you can use one of several
    other packages. I'm personally a big fan of DOM XML. It's reasonably
    easy to use and seems to work pretty well. Check out:

    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


    Comment

    Working...