XML to HTML Conversion & Display - html

How Can I display the below XML into an HTML page: http://weather.maltairport.com/xml/WeatherReport_21032014.xml
XML Sample:
<WeatherForecast>
<date>2014-03-21</date>
<publishedtime>17:00:14</publishedtime>
<forecast>
<time>16:40</time>
<forecastvalid>Outlook for Tomorrow</forecastvalid>
<conditiontoday>SUNNY</conditiontoday>
<temperature>16</temperature>
<humidity>76</humidity>
<atmospressure>1021</atmospressure>
<wind>SE 8 Knots</wind>
<sunrise>06:05</sunrise>
<forecastimage>
http://weather.maltairport.com/assets/img-design/weatherconditions/52.png
</forecastimage>
<sunset>18:14</sunset>
<dayforecast day="1">
<forecastdate>2014-03-22</forecastdate>
<condition>MAINLY CLOUDY</condition>
<high>17</high>
<low>12</low>
<heat_stress>17</heat_stress>
<wind_direction>ESE</wind_direction>
<wind_force>F3 --> SSE F3 to 4</wind_force>
<uvindex>5</uvindex>
<forecastimage>
http://weather.maltairport.com/assets/img-design/weatherconditions/21.png
</forecastimage>
</dayforecast>
I am a newbie to these types of XML files so any help/guidance is truly appreciated.

You need a couple things:
A) An XSL file that transforms the XML into an output type of your choice, in this case HTML.
B) A PHP (or other) script to merge and process the transform. I use the following script in a project of mine:
<?php
$xml = new DOMDocument;
$xml->load('index.xml');
$xsl = new DOMDocument;
$xsl->load('index.xsl');
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
echo $proc->transformToXML($xml);
?>

Related

Convert inline styles to <font> tags including attributes

I'm working with a legacy system that does not support inline style or CSS input. There is a large number of HTML files that need to be converted to a specific format to be compatible with this system.
So I'm looking for a way to progrmmatically convert the inline styles of several tags to individual font tags with the relevant attributes.
Can this be done? For example:
<p style="color: #000; font-family: arial,helvetica,sans-serif; font-size: 10px; font-weight: bold;">Text</p>
<!-- Converted to: -->
<font color="#000" face="arial,helvetica,sans-serif" size="10px" weight="bold"><p>Text</p></font>
EDIT: Not a duplicate of What tag should I use instead of deprecated tag font in html (cannot use CSS). I am not looking for an alternative to the font tag since style attributes get stripped in the legacy system.
Yes, it can be done. You need a programmable scripting language that can read/write text files, perform basic logic, loops, etc. There are several to choose from, varying in level of difficulty. Examples include:
VBA
Windows PowerShell
PHP (install XAMPP and you will have access to this powerful language)
node.js
python
WinBatch
AutoHotKey (AHK) or AutoIT (AHK forked off from AutoIT)
For ease of use, my preferences would be ranked in this order: 6, 3, 5, 1, 4, 2, 7.
Pick one, then begin trying to do the project, then come back and ask us for more help if you are stuck. Basically, a pseudo-code algorithm might look something like this:
arr = array_of_the_html_filenames
for i = 1 to len(arr) //i.e. do this for each filename
next_file_name = arr[i]
func_process_this_file(next_file_name)
next
func_process_this_file(file_name)
input_file_name = file_name
output_file_name = parse input_file_name string to create an output_file_name
hFIN = fileOpen(input_file_name, "read") #get fileHandle for next file
hFOUT = fileOpen(output_file_name, "write")
next_line = fileRead(hFIN) //read next_line of current file as a string
while next_line !== "EOF"
out_line = ''
if next_line == EOF: break
if next_line contains "font-family":
font_data = parse the string to get the data for the font tag
rest_of_string_with_font_data_removed = parse string to extract all except font data
out_line = "<font>" + font_data + "</font>" + rest_of_string_with_font_data_removed
file_write(hFOUT, out_line)
else
out_line = next_line
file_write(hFOUT, out_line)
endif
next_line = fileRead(hFIN) //read next_line of current file as a string
endwhile
file_close(hFIN)
file_close(hFOUT)
return

Can I include an image in an Excel report created from a sql query?

I have to create an Excel report, based on a mysql query, which contains text columns. One of these columns stores the absolute path of an image. I'm wondering if would be possible to create the report including the image visible (as ole?).
Basically, in Excel, can I show the image If I have its path?
You can do this with PHPExcel. Firstly you need to download the latest version: https://phpexcel.codeplex.com/
And here is an example of it's use:
include 'PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set properties
$objPHPExcel->getProperties()->setCreator("Jobin Jose");
$objPHPExcel->getProperties()->setLastModifiedBy("Jobin Jose");
$objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHPExcel classes.");
// Add some data
// echo date('H:i:s') . " Add some data\n";
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('B2', 'world!');
//$objPHPExcel->getActiveSheet()->SetCellValue('C1', 'Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('D2', 'world!');
$objPHPExcel->getActiveSheet()->setTitle('Simple');
$gdImage = imagecreatefromjpeg('uploads/t12.jpg');
// Add a drawing to the worksheetecho date('H:i:s') . " Add a drawing to the worksheet\n";
$objDrawing = new PHPExcel_Worksheet_MemoryDrawing();
$objDrawing->setName('Sample image');
$objDrawing->setDescription('Sample image');
$objDrawing->setImageResource($gdImage);
$objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG);
$objDrawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
$objDrawing->setHeight(150);
$objDrawing->setCoordinates('C1');
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
// Echo done
echo date('H:i:s') . " Done writing file.\r\n";
The above code will create an “xlsx” formatted file because it uses 2007 excel classes If you want “xls” format just try with 2005 class do not for get to change the file format to “xls” while using 2005.
See here for an example

How to use "Process Document From File" operator of RapidMiner in Java Code

I have just started using rapid miner for text classification. I have created a process in which i used "Process Document from Files" operator for tf-idf conversion. I want to ask how to use this operator in Java code ? I search on internet but all are using the already created process or word list generated from documents ? I want to start it from scratch i.e.
1 ) Process Documents From File
1.1) Tokenization
1.2) Filtering
1.3) Stemming
1.4) N-Gram
2) Validation
2.1) Training (K-NN)
2.2) Apply Model
May be source code and image below can help You:
String processDefinitionFileName = "/home/maximk/.RapidMiner5/repositories/Local Repository/processes/processOpenCSV.rmp";
File processDefinition = new File( processDefinitionFileName );
Process readCSV = new Process( processDefinition );
File csvFile = new File( "/home/maximk/test.cvs" );
IOObject inObject = new SimpleFileObject( csvFile );
IOContainer inParameters = new IOContainer( inObject );
IOContainer outParameters = readCSV.run( inParameters );
SimpleExampleSet resultDataSet = (SimpleExampleSet) outParameters.getElementAt( 0 );

How can I extract values, from specific tags, from an XML file into an HTML page?

I've got an XML file.
<key>457</key>
<dict>
<key>Track ID</key><integer>457</integer>
<key>Name</key><string>Love me do</string>
<key>Artist</key><string>The Beatles</string>
<key>Album Artist</key><string>The Beatles</string>
<key>Composer</key><string>John Lennon/Paul McCartney</string>
<key>Album</key><string>The Beatles No.1</string>
<key>Genre</key><string>Varies</string>
<key>Kind</key><string>AAC audio file</string>
</dict>
I've removed for these purposes a lot of the file (this is one song, and there are about 20-30 more lines of XML per song). What I'd like to do is extract the 'Artist' string from each song, and then remove all of the repeated strings, and then take that and output it into an HTML file; preferably in a way that autorefreshes when a new version of the .xml is found, thus keeping an updated file, but if that overcomplicates it, that's fine.
I've looked into ways with doing it with jQuery, and I've had PHP suggested, but I'm unsure of which is the better/cleaner; and I'm unsure how I would go about doing it in either.
Many thanks,
Henry.
I would do this in PHP: put your XML into a string, then (because only you are going to use this), encode it to JSON, decode it into an assoc array, then run a foreach loop to extract the artists, and finally remove the duplications, and then save it as an HTML. Then, you can add a cron job to run this periodically, and generate the HTML. Run this code, then link to the results that it gives out.
$contents = '<key>Blah.... lots of XML';
$xml = simplexml_load_string($contents);
$json = json_encode($xml);
$array = json_decode($json, true);
print_r($array);
Once I know the structure of the array that is produced, I can complete the code. But it would look something like this:
foreach($array['dict']['artist'] as $artist) {
$artists[] = $artist;
}
// Now $artists holds an array of the artists
$arists = array_unique($artists);
// Now there are no duplicates
foreach($artists as $artist) {
$output .= '<p>',$artist,'</p>';
}
// Now each artist is put in it's own paragraph.
// Either output the output
echo $output;
// Or save it to a file (in this case, 'artists.html')
$fh = fopen('artists.html', 'w') or die("Can't open file");
fwrite($fh, $output);
fclose($fh);
This does not work completely get as the line in the first foreach loop needs a bit of tweaking, but this is a starting point.
What exactly are you trying to achieve? If you need HTML files that are periodically regenerated based on the XML files, then you probably want to write a program (for example, the BeautifulSoup Python library allows you to parse XML/HTML files quite easily) for it and run it every time you need to update the HTML files (you can also set up a cron job for it).
If you need to be able to fetch the data from XML on the fly, you can use some JavaScript library and load the XML from an xml file, then add it to the page dynamically.
For example, this Python program will parse an XML file (file.xml) and create an HTML file (song_information.html) that contains data from the XML file.
from BeautifulSoup import BeautifulStoneSoup
f = open("file.xml")
soup = BeautifulStoneSoup(f.read())
f.close()
html = """<!DOCTYPE html>
<html>
<head>
<title>Song information</title>
</head>
<body>
"""
for key in soup.dict.findAll('key'):
html += "<h1>%s</h1>\n" % key.contents[0]
html += "<p>%s</p>\n" % key.nextSibling.contents[0]
html += """</body>
</html>
"""
f = open("song_information.html", "w")
f.write(html)
f.close()
It will write the following HTML to the song_information.html file:
<!DOCTYPE html>
<html>
<head>
<title>Song information</title>
</head>
<body>
<h1>Track ID</h1>
<p>457</p>
<h1>Name</h1>
<p>Love me do</p>
<h1>Artist</h1>
<p>The Beatles</p>
<h1>Album Artist</h1>
<p>The Beatles</p>
<h1>Composer</h1>
<p>John Lennon/Paul McCartney</p>
<h1>Album</h1>
<p>The Beatles No.1</p>
<h1>Genre</h1>
<p>Varies</p>
<h1>Kind</h1>
<p>AAC audio file</p>
</body>
</html>
Of course, this is simplified. If you need to implement unicode support, you will want to edit it like this:
from BeautifulSoup import BeautifulStoneSoup
import codecs
f = codecs.open("file.xml", "r", "utf-8")
soup = BeautifulStoneSoup(f.read())
f.close()
html = """<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Song information</title>
</head>
<body>
"""
for key in soup.dict.findAll('key'):
html += "<h1>%s</h1>\n" % key.contents[0]
html += "<p>%s</p>\n" % key.nextSibling.contents[0]
html += """</body>
</html>
"""
f = codecs.open("song_information.html", "w", "utf-8")
f.write(html)
f.close()
Also, you will probably need to generate more complex HTML, so you will likely want to try some template systems like Jinja2.

Combine PDF Blob Files from MySQL Database

How do I combine numerous PDF blob files into a single PDF so that can then be printed?
<?php
include 'config.php';
include 'connect.php';
$session= $_GET[session];
$query = "
SELECT $tbl_uploads.username, $tbl_uploads.description,
$tbl_uploads.type, $tbl_uploads.size, $tbl_uploads.content,
$tbl_members.session
FROM $tbl_uploads
LEFT JOIN $tbl_members
ON $tbl_uploads.username = $tbl_members.username
WHERE $tbl_members.session= '$session'";
$result = mysql_query($query) or die('Error, query failed');
while(list($username, $description, $type, $size, $content) =
mysql_fetch_array($result))
{
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: inline; filename=$username-$description.pdf");
}
echo $content;
mysql_close($link);
exit;
?>
How do I combine numerous PDF blob files into a single PDF so that can then be printed?
You don't - at least not by simply combining the byte streams. You will need to process each file, and merge them into a new PDF document.
Some pointers, maybe one of the solutions, depending on the platform you're on, works for you:
PHP - How to combine / merge multiple pdf’s
How to combine images inside pdf by programme?
Need to merge multiple pdf’s into a single PDF with Table Of Contents sections
pdftk will merge multiple PDF files, as well as PDFsam..