How to convert a HTML file to XML file?

Taking data from an XML file and putting it into an HTML file.

  • 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 http://weather.szekeres.org/weather.xml that has some weather station info (temp, humidity, etc.) that I want to incorporate into http://weather.szekeres.org/wx.html. I haven't the slightest idea how to accomplish that. Help me please!

  • Answer:

    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?

@homer at Ask.Metafilter.Com Visit the source

Was this solution helpful to you?

Other answers

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. ;)

@homer

For the example you've given, http://search.cpan.org/dist/XML-Simple/lib/XML/Simple/FAQ.pod 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.

i_am_joe's_spleen

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 http://weston.canncentral.org/misc/xml-css/addressdata.xml. 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 http://weston.canncentral.org/misc/xml-css/dataformat.css. An HTML page like http://weston.canncentral.org/misc/xml-css/displayframe.html 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.

weston

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.

@homer

i'm no xml genius, but the stylesheet approach shouldn't require changing how your app outputs the xml...

juv3nal

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...

@homer

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?

weston

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, http://www.w3schools.com/xsl/xsl_transformation.asp. This http://www.w3schools.com/xsl/xsl_client.asp 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.

bobo123

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.

bobo123

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.