Solved

Need to convert XML file into HTML using PHP?

Posted on 2014-03-15
3
1,164 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
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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Append letter to end of string 4 36
php call to a non-object 3 34
Php pie charts 3 26
Form not operating correctly. 1 23
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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 …
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…
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 …

867 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

21 Experts available now in Live!

Get 1:1 Help Now