?
Solved

Need to convert XML file into HTML using PHP?

Posted on 2014-03-15
3
Medium Priority
?
1,314 Views
Last Modified: 2014-03-15
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
Comment
Question by:sabecs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39932033
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
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 39932081
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
 

Author Closing Comment

by:sabecs
ID: 39932090
Thanks  for your help, much appreciated.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question