jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes modelling html Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "modelling html" Watch "modelling html" New topic

modelling html

timothy zimmerman
Ranch Hand

Joined: Jun 26, 2001
Posts: 149
Does anyone have any suggestions as to how I could go about modelling html as java object(s)? The problem is this: I have some objects which I would like to render as a set of html pages. I would like to pass the objects to a renderer that would then take information from the objects and build html pages linking from one page to another where appropriate.
My initial thought was to write some set of java objects to represent the various html elements with the ability to show themselves as html (ie toString()?). However, it seems like that may be a lot to hand code if I want to cover all (or most of) the elements supported by html.
My next thought was along the lines of the first but to use some xml schema/java generation tool (ie jaxb or castor?)
Any suggestions or help would be welcome. Also, is there anything like this out there already? I have tried a few searches and have not found anything.
frisode jonge
Ranch Hand

Joined: Dec 30, 2003
Posts: 34
you may want to take a look at xslt and xpath. Those tools can render any xml (for instance html) differently depending on the stylesheet and make it either a java class or other.
I am not sure if this is what you mean...
Tim West
Ranch Hand

Joined: Mar 15, 2004
Posts: 539
I'd go with rendering the objects as XML and then translating from there, perhaps with XSLT. Generated XML is more meaningful than HTML - it's logical markup rather than presentational markup. Later on in the project the XML may be useful - for persistence or translating into another format like PDF.
In terms of system architecture, it depends on the structure of the objects you're translating from. If you have a composite structure (I'm thinking like composite pattern...), the visitor pattern is ideal generating a particular output.
There are probably other good (better?) options but thats the way I'd approach it.

timothy zimmerman
Ranch Hand

Joined: Jun 26, 2001
Posts: 149
Thanks for the input. I defintely think I want the output to be XML but I think maybe I didn't explain clearly enough ... or don't completely understand the answer.
The task is to generate HTML reports based on java objects. What I would like to do is pass the java object to some type of renderer which will know how and what to pull off of the object and build an HTML object. Not necessarily an HTML page or xml file but a java object that has members such as body whcih in turn might have member collections of other HTML 'element objects' such as <a href...
If I could define a XML Schema for these HTML objects using something like Casstor, I could then simply marshall the objects to an XML file and then process it using XSLT.
Does it make sense? Is this a crazy idea?
thanks again
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24190

Not even close to crazy. A perfectly reasonable thing to do. Why reinvent the wheel? (X)HTML is XML.

[Jess in Action][AskingGoodQuestions]
timothy zimmerman
Ranch Hand

Joined: Jun 26, 2001
Posts: 149
Again I think I have not gotten my ont across accurately. I understand XHTML and HTML. I do not intend to reinvent XHTML. What I would like to do is use the XHTML schema to generate java objects which represent HTML elements. Then I can marshall these objects to XML which would be actually be XHTML.
I have tweaked the XHTML schema enough to generate the HTML Java objects. So now I have runtime objects such as Html and Body which I can load with data ... but Iam not sure the marshall method is going to give me the pages I want.
Jaap van Hengstum

Joined: Jul 24, 2003
Posts: 24
You might also want to take a look at some of the html-templating languages for Java, like Velocity or Freemarker. These are especially tailored for creating html representations of java objects, the advantages over XML/XSL being simpler to use and no need to convert your java objects to XML first (a disadvantage is that they are more java-platform dependent).
You could f.e. just have a generic renderer object that takes a javabean (the model) and a template string/stream (the view) and generates the html.
Lasse Koskela

Joined: Jan 23, 2002
Posts: 11962
Just wondering... Did you look whether you could've reused some of the HTML parsing code in HtmlUnit?

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Byron Estes
Ranch Hand

Joined: Feb 21, 2002
Posts: 313
Have you looked at JAXB?
It's sole purpose in life is to be able to take XML and map it to an object graph, or vice versa.
After that consider something like Jakarta Cacoon that can take an XML data document and using XSLT in it's framework, with a set of renderers, pump out all sorts of views/formats (e.g. wml, hdml, pdf, html etc.).
[ March 26, 2004: Message edited by: Byron Estes ]

Byron Estes<br />Sun Certified Enterprise Architect<br />Senior Consulant<br />Blackwell Consulting Services<br />Chicago, IL<br /><a href="http://www.bcsinc.com" target="_blank" rel="nofollow">www.bcsinc.com</a>
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: modelling html
jQuery in Action, 3rd edition