Taking data from an XML file and putting it into an HTML file.
March 9, 2004 7:22 PM   Subscribe

I need help taking data from an XML file and putting it into an HTML file. (MI)

I have basically minimal knowledge of HTML and none having to do with XML. I have a data file that has some weather station info (temp, humidity, etc.) that I want to incorporate into this page. I haven't the slightest idea how to accomplish that. Help me please!
posted by @homer to Computers & Internet (16 answers total)
 
You could use Perl with any of the XML libraries, or Python with ditto, or (my preference in this case) XSLT to transform the XML into HTML. It all depends on what is available in your system. Maybe a little more information on your setup?
posted by brool at 7:45 PM on March 9, 2004


I'm on a shared host. From what I see with the installed perl modules there are a boatload of xml modules installed. Obviously my skills are lacking. ;)
posted by @homer at 8:05 PM on March 9, 2004


For the example you've given, XML::Simple would do all that you need.

If you don't have the Perl-fu, anyone with minimal perl hacking skills could do it in less than an hour. Including me, ask nice if you haven't got any help by Friday and I can hack it up over the weekend.
posted by i_am_joe's_spleen at 8:21 PM on March 9, 2004


One interesting possibility would be to put an iframe in the page, and point that to an XML document with a CSS stylesheet linked in which designates how elements are to be displayed. I've played a little bit with this and it seemed to work well enough.

Example:

Consider this bit of xml data. If you pull it up, you'll notice it actually displays with some formatting, and a peek at the source would reveal that's because it links in this stylesheet. An HTML page like this one could actually be used to frame it.

Pros: no knowledge of any particular scripting language needed. The browser does the formatting. No script-fu required. Cons: as I said, browser support is spotty and a pretty good knowledge of CSS is needed. I know a fair bit and you can see how I've got some of the formatting wrong. So, browser-fu required.

But it's fun to think about if you're a browser geek, and the trick could come in handy someday.

Me, I'm still hoping that someday we can just make up our own freakin' tags willy-nilly as we go along, and mix them with standard XHTML for a delicious tag soup blend like no other.
posted by weston at 8:30 PM on March 9, 2004


Many thanks to all. The stylesheet route wont work because I can't change how the app outputs the XML file. As for the XML::Simple option my pea brain just about exploded with it. I haven't the slightest idea what any of it means.
posted by @homer at 9:09 PM on March 9, 2004


i'm no xml genius, but the stylesheet approach shouldn't require changing how your app outputs the xml...
posted by juv3nal at 9:16 PM on March 9, 2004


I thought the stylesheet had to be specified in the XML file? It looks that way to me anyway in the above example. I could be and probably am wrong...
posted by @homer at 9:37 PM on March 9, 2004


I thought the stylesheet had to be specified in the XML file?

It does, but it's pretty easy to do so. Fire up a text editor, open your XML file within it, and below this line:

<?xml version="1.0" encoding="ISO-8859-1"?>

(which most XML docs should have some equivalent of)

Add this line:

<?xml-stylesheet type="text/css" href="dataformat.css"?>

with the href, of course, pointing to your style sheet, which will have to have definitions which correspond to your tags, rather than my <record>, <lastname>, etc tags.

All previous disclaimers about this being an iffy approach still apply, though.

I hear, by the way, that with IE you can actually apply an XSLT style sheet rather than a CSS style sheet and again have the browser do the transformation/display work. Anyone know if this is true?
posted by weston at 10:14 PM on March 9, 2004


I hear, by the way, that with IE you can actually apply an XSLT style sheet rather than a CSS style sheet and again have the browser do the transformation/display work. Anyone know if this is true?

Yup, example near bottom of page.

This example looks interesting, using javascript to pull a xml file and a xsl file and display it in a html page. The tricky part would be writing the xsl which is not exactly trivial but shouldn't be too hard.
posted by bobo123 at 12:38 AM on March 10, 2004


Okay, been playing around here...

Here's an example weather.xsl:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="ws2300">
<html>
<body>
<h2>Today's Weather</h2>
<p>
Date: <xsl:value-of select="Date" /><br />
Time: <xsl:value-of select="Time" /><br />
Indoor Temperature:
<xsl:value-of select="Temperature/Indoor/Value" />
</p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

... and here's an example of your weather.html:

<html>
<body><script type="text/javascript">
// Load XML
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false
xml.load("weather.xml")

// Load XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("weather.xsl")

// Transform
document.write(xml.transformNode(xsl))</script>

</body>
</html>

... and this should work if they're in the same directory as weather.xml. Personally I'd rather do it on the server (this won't work with Mozilla) but this should get you started, extending the XSL shouldn't be too difficult. Been drinking so excuse any obvious mistakes.
posted by bobo123 at 1:23 AM on March 10, 2004


I thought the stylesheet had to be specified in the XML file? It looks that way to me anyway in the above example. I could be and probably am wrong...

my bad. i suppose post-processing the xml file to put in the css reference is out of the question then. if that's viable it still sounds easier to me than extracting the data and dumping it into an html page.
posted by juv3nal at 2:33 AM on March 10, 2004


Actually, if you made the xml file an include in an HTML file, you wouldn't need to alter it (would you?) and could apply the CSS that way.

I don't have the programming chops either, but I'd agree that generating an HTML file using XSLT or something at the server side would be the best option.
posted by adamrice at 6:44 AM on March 10, 2004


I hear, by the way, that with IE you can actually apply an XSLT style sheet rather than a CSS style sheet and again have the browser do the transformation/display work. Anyone know if this is true?

Yes, and in Mozilla. I would suggest doing that here, but I think it's going to be a lot easier to learn XML::Simple or something akin thereto than to learn XSLT.
posted by IshmaelGraves at 8:10 AM on March 10, 2004


Thanks folks. It gives me something to chew on!
posted by @homer at 8:35 AM on March 10, 2004


@homer, I have just what you need, sadly it's on a serial ata drive in a computer whose CPU has been sent for testing, and the other computers in my house can't use SATA. So if you don't mind, I'll send you my weather stuff once I have my main PC up and running.
posted by riffola at 2:44 PM on March 10, 2004


If you don't mind using PHP, you can use spectre013's scripts to parse the weather.com data.
posted by riffola at 2:48 PM on March 10, 2004


« Older Online Ad Rates   |   Protecting My Property Newer »
This thread is closed to new comments.