Solved

Need to convert XML file into HTML using PHP?

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

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
geting data from the array list 6 37
Code planning methods/tools? 5 56
Joomla Editing 3 21
php problem trying to see the diffrence in logic between 2 pages 1 23
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

734 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