I am trying to display information from a XML file. I am using PHP and its DOMDocument class. I am attaching the xml file and the php code that i have tried.
<?xml version="1.0" encoding="utf-8" ?>
<services>
<service>
<type>Wash</type>
<title>Wash1</title>
<content>Full Exterior Hand Wash</content>
<content>Chamois dry, Pressure Clean Wheels, All Door Jambs</content>
<content>Plus Free Typre Gloss</content>
<price>Hatch/Sedan:$15</price>
<price>Others:$20</price>
</service>
<service>
<type>Wash</type>
<title>Wash2</title>
<content>Wash1</content>
<content>Plus Vacuum</content>
<content>Glass Cleaned(IN/OUT)</content>
<content>Plus Free Interior Wipe</content>
<price>Hatch/Sedan:$30</price>
<price>Others:$38</price>
</service>
</services>
The php code...
<?php
header('Content-Type: text/html;charset=utf8');
$xmlfile = new DOMDocument();
$xmlfile->load('services.xml');
$services = $xmlfile->getElementsByTagName('service');
foreach($services as $service){
$titles = $service->getElementsByTagName('title');
$title = $titles->item(0)->nodeValue;
echo "<h1>$title</h1>";
$prices = $xmlfile->getElementsByTagName('price');
foreach($prices as $price){
$price = $prices->item(0)->nodeValue;
echo "<h1>$price</h1>";
}
}
?>
I am only trying to output price tag values, since if that works displaying content shouldn't be a problem. Eventually i will be displaying these values in HTML structure, one by one.
Thanks for your help.
Edit: This code at the moment outputs
Wash1
Hatch/Sedan:$15
Hatch/Sedan:$15
Hatch/Sedan:$15
Hatch/Sedan:$15
Wash2
Hatch/Sedan:$15
Hatch/Sedan:$15
Hatch/Sedan:$15
Hatch/Sedan:$15