Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1377
  • Last Modified:

Need to convert XML file into HTML using PHP?

Hi,
is there an easy way to create a select list from an XML file using PHP?

<select name="shipping_name">
<option value="Toll Road Express ATL,27.64">+ Toll Road Express ATL - $27.64</option>
<option value="Toll Road Express,33.14">+ Toll Road Express - $33.14</option>
<option value="StarTrack Local ATL,35.17>+ StarTrack Local ATL - $35.17</option>
...
..
.
</select>

Open in new window


from

<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Status>OK</Status>
<ReplyType>Rates</ReplyType>
<Shipment>
<Collection>
<City>Niddrie</City>
<PostCode>3042</PostCode>
<Country>Australia</Country>
<CountryCode>AU</CountryCode>
</Collection>
<Delivery>
<City>Melbourne</City>
<PostCode>3001</PostCode>
<Country>Australia</Country>
<CountryCode>AU</CountryCode>
</Delivery>
<Package>
<Weight>4</Weight>
<Length>43</Length>
<Width>46</Width>
<Height>66</Height>
</Package>
<Package>
<Weight>2</Weight>
<Length>43</Length>
<Width>12</Width>
<Height>12</Height>
</Package>
</Shipment>
<Details>
<Currency></Currency>
<Taxable>Y</Taxable>
<TaxPercentage>10</TaxPercentage>
<InvoiceRequired>N</InvoiceRequired>
</Details>
<Rates>
<Service>
<Name>Toll Road Express ATL</Name>
<Carrier>Toll</Carrier>
<PrinterRequired>y</PrinterRequired>
<TransitCover>0</TransitCover>
<Price>25.13</Price>
<Tax>2.51</Tax>
<Total>27.64</Total>
</Service>
<Service>
<Name>Toll Road Express</Name>
<Carrier>Toll</Carrier>
<PrinterRequired>y</PrinterRequired>
<TransitCover>100</TransitCover>
<Price>30.13</Price>
<Tax>3.01</Tax>
<Total>33.14</Total>
</Service>
<Service>
<Name>StarTrack Local ATL</Name>
<Carrier>StarTrack</Carrier>
<PrinterRequired>y</PrinterRequired>
<TransitCover>0</TransitCover>
<Price>31.97</Price>
<Tax>3.20</Tax>
<Total>35.17</Total>
</Service>
<Service>
<Name>TNT Road Express</Name>
<Carrier>TNT</Carrier>
<PrinterRequired>y</PrinterRequired>
<TransitCover>100</TransitCover>
<Price>32.45</Price>
<Tax>3.25</Tax>
<Total>35.70</Total>
</Service>
<Service>
<Name>StarTrack Local</Name>
<Carrier>StarTrack</Carrier>
<PrinterRequired>y</PrinterRequired>
<TransitCover>100</TransitCover>
<Price>38.47</Price>
<Tax>3.85</Tax>
<Total>42.32</Total>
</Service>
<Service>
<Name>Toll IPEC Priority ATL</Name>
<Carrier>Toll</Carrier>
<PrinterRequired>y</PrinterRequired>
<TransitCover>0</TransitCover>
<Price>45.82</Price>
<Tax>4.58</Tax>
<Total>50.40</Total>
</Service>
<Service>
<Name>Toll IPEC Priority</Name>
<Carrier>Toll</Carrier>
<PrinterRequired>y</PrinterRequired>
<TransitCover>100</TransitCover>
<Price>50.82</Price>
<Tax>5.08</Tax>
<Total>55.90</Total>
</Service>
<Service>
<Name>TNT Overnight</Name>
<Carrier>TNT</Carrier>
<PrinterRequired>y</PrinterRequired>
<TransitCover>100</TransitCover>
<Price>55.40</Price>
<Tax>5.54</Tax>
<Total>60.94</Total>
</Service>
<Service>
<Name>Interparcel Domestic Express</Name>
<Carrier>DHL</Carrier>
<PrinterRequired>y</PrinterRequired>
<TransitCover>100</TransitCover>
<Price>66.08</Price>
<Tax>6.61</Tax>
<Total>72.69</Total>
</Service>
<Service>
<Name>Interparcel Domestic 9am</Name>
<Carrier>DHL</Carrier>
<PrinterRequired>y</PrinterRequired>
<TransitCover>100</TransitCover>
<Price>74.49</Price>
<Tax>7.45</Tax>
<Total>81.94</Total>
</Service>
<Service>
<Name>Interparcel Domestic 12pm</Name>
<Carrier>DHL</Carrier>
<PrinterRequired>y</PrinterRequired>
<TransitCover>100</TransitCover>
<Price>77.52</Price>
<Tax>7.75</Tax>
<Total>85.27</Total>
</Service>
<Service>
<Name>UPS Domestic Express</Name>
<Carrier>UPS</Carrier>
<PrinterRequired>y</PrinterRequired>
<TransitCover>100</TransitCover>
<Price>111.47</Price>
<Tax>11.15</Tax>
<Total>122.62</Total>
</Service>
</Rates>
</Response>

Open in new window

0
sabecs
Asked:
sabecs
1 Solution
 
Ray PaseurCommented:
Easy?  That's "relative."

Please have a look at this script fragment.  Using OOP you may be able to tease out the data elements you need to create the HTML from the XML document.
http://www.iconoun.com/demo/temp_sabecs.php
0
 
leakim971PluritechnicianCommented:
THe following code :
<?php

if(file_exists('file.xml'))
{
	$xml = simplexml_load_file('file.xml');
	$result = $xml->xpath('/Response/Rates/Service');

	$options = [];
	while(list( , $node) = each($result)) {
		$name = (String)$node->Name[0];
		$total = (String)$node->Total[0];
		$options[] = "<option value='" . $name . ", " .  $total . "'>+ " . $name . " - " . $total . "</option>"; 
	}
	$options = implode("", $options);

	echo "<select name='shipping_name'>" . $options . "</select>";

}
else
{
	exit('Failed to open file.');
}

?>

Open in new window


Generate this :

<select name='shipping_name'>
<option value='Toll Road Express ATL, 27.64'>+Toll Road Express ATL - 27.64</option><option value='Toll Road Express, 33.14'>+Toll Road Express - 33.14</option>
<option value='StarTrack Local ATL, 35.17'>+StarTrack Local ATL - 35.17</option>
<option value='TNT Road Express, 35.70'>+TNT Road Express - 35.70</option>
<option value='StarTrack Local, 42.32'>+StarTrack Local - 42.32</option>
<option value='Toll IPEC Priority ATL, 50.40'>+Toll IPEC Priority ATL - 50.40</option><option value='Toll IPEC Priority, 55.90'>+Toll IPEC Priority - 55.90</option>
<option value='TNT Overnight, 60.94'>+TNT Overnight - 60.94</option>
<option value='Interparcel Domestic Express, 72.69'>+Interparcel Domestic Express - 72.69</option>
<option value='Interparcel Domestic 9am, 81.94'>+Interparcel Domestic 9am - 81.94</option>
<option value='Interparcel Domestic 12pm, 85.27'>+Interparcel Domestic 12pm - 85.27</option>
<option value='UPS Domestic Express, 122.62'>+UPS Domestic Express - 122.62</option>
</select>

Open in new window

0
 
sabecsAuthor Commented:
Thanks  for your help, much appreciated.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now