xml dom to read a file

M

mr_burns

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
 
M

Martin Honnen

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";
}
?>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top