Thursday, November 15, 2012

Answers > How Do I Parse a XML File and Create an Equvalent HTML File and Vice Versa?

How Do I Parse a XML File and Create an Equvalent HTML File and Vice Versa?

by nForms on December 15, 2011

I need to parse a XML file using java and create an equvalent HTML file and also need to parse a HTML file and generate an equvalent XML file.
Thank you. Code would be appriciated...or the ways to do it.

Tags: , ,

Similar XForms | XML Posts

{ 5 comments… read them below or add one }

elven_rangers July 13, 2011 at 4:48 pm

There is no such thing as an ‘equivalent HTML file’ for an XML file. HTML is a form of XML, any browser can display XML as a normal HTML page, there’s nothing special you need to do.

If you want to display it as html simply point your browser to the XML file just as it is (or make a link to it or whatever) and it will be displayed, tag by tag, item by item.

If you need special formatting, then you can simply take the info from the XML and manually format it with CSS into a normal HTML.

Note: you shouldn’t change the extension since then the browser will try to read the XML as HTML, but it won’t recognize the tags so you’ll simply get a dump of the text inside tags.

marcusang2345 July 13, 2011 at 4:48 pm

To make it an HTML file,try changing the file extension of the file to .html,or copy the whole code and paste it in another program that uses the same code and saves it as an HTML file.=)

Garry July 13, 2011 at 4:48 pm
plankton July 13, 2011 at 4:48 pm

As I see it you have three options, which one you choose depends on the exact requirements…

1. Parse the XML into a DOM document and use DOM methods to find your elements and transform – probably the easiest method for smaller documents.

2. Parse the XML using SAX and transform based on events – best for large XML documents and efficient resource use.

3. Do an XSLT transformation – possibly the hardest to perfect but well worth the effort.

For 1 & 2 try the following tutorial…

http://www.totheriver.com/learn/xml/xmltutorial.html

For 3, the following page should give you everything you need to get started…

http://www.oreillynet.com/pub/a/oreilly/java/news/javaxslt_0801.html

For the HTML to XML you can use the same methods, just make sure the HTML is valid (use HTML Tidy) or you’ll have all sorts of problems…

http://tidy.sourceforge.net/

You can get the java implementation from the ‘Source Forge Projects’ menu on the right-hand-side of the page.

Hope this helps

KELLY W July 13, 2011 at 4:48 pm

XML Notepad 2007

Previous post:

Next post: