Solved

Need to convert XML file into HTML using PHP?

Posted on 2014-03-15
3
1,137 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
3 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
Thanks  for your help, much appreciated.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now