xml dom to read a file

Discussion in 'XML' started by mr_burns, Aug 6, 2004.

  1. mr_burns

    mr_burns Guest

    hi,

    i am currently tryin to learn enough xml dom to read a simple xml file
    and output the contents using php. at the moment i am reading a book
    (professional php4 xml) but i am running out of time as i kinda need
    to get it up asap. below is how the xml file will be presented:

    <?xml version="1.0" encoding="iso-8859-1"?>
    <products>
    <item name="gold">
    <price>15.00</price>
    </item>
    <item name="silver">
    <price>12.50</price>
    </item>
    <item name="bronze">
    <price>10.00</price>
    </item>
    </products>

    if its not too much code, could someone please tell me how i would do
    this? i only need to read the file and print the output. the reason i
    have asked for xml dom is because i have checked that the server has
    this enabled (i have learned that much). otherwise, are their any
    short tutorials on this? cheers

    burnsy
    mr_burns, Aug 6, 2004
    #1
    1. Advertising

  2. mr_burns wrote:


    > i am currently tryin to learn enough xml dom to read a simple xml file
    > and output the contents using php. at the moment i am reading a book
    > (professional php4 xml) but i am running out of time as i kinda need
    > to get it up asap. below is how the xml file will be presented:
    >
    > <?xml version="1.0" encoding="iso-8859-1"?>
    > <products>
    > <item name="gold">
    > <price>15.00</price>
    > </item>
    > <item name="silver">
    > <price>12.50</price>
    > </item>
    > <item name="bronze">
    > <price>10.00</price>
    > </item>
    > </products>
    >
    > if its not too much code, could someone please tell me how i would do
    > this? i only need to read the file and print the output.


    Here is an example:

    <?php
    $xml_document = domxml_open_file('test2004080602.xml');

    if ($xml_document) {
    $items = $xml_document->get_elements_by_tagname('item');
    $itemCount = count($items);
    echo "<ul>\r\n";
    for ($i = 0; $i < $itemCount; $i++) {
    $item = $items[$i];
    $prices = $item->get_elements_by_tagname('price');
    $price = $prices[0];
    if ($price) {
    $price = $price->first_child();
    echo '<li>' . $item->get_attribute('name') . ' costs ' .
    $price->node_value() .
    "</li>\r\n";
    }
    }
    echo "</ul>\r\n";
    }
    ?>

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Aug 6, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    532
  2. =?Utf-8?B?ZGF2aWQ=?=
    Replies:
    1
    Views:
    284
    bruce barker
    Mar 8, 2007
  3. Replies:
    3
    Views:
    510
    Stefan Behnel
    Aug 3, 2007
  4. Alan
    Replies:
    6
    Views:
    1,555
  5. Alex Dowad
    Replies:
    4
    Views:
    248
    Michel Demazure
    May 1, 2010
Loading...

Share This Page