Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

Using Java to produce HTML like a php script

Thread view: 
spudtheimpaler@gmail.com - 19 Jan 2006 19:28 GMT
I am a university student using a program that requires updated xml
from a webpage (well, a webserver).  Now i know this can be done with
php and apparently quite easily but I would really rather do this with
Java (as the program that will be manipulating the information to be
described  is going to be in java if possible).

So my question is are there any tools / apis that are good at
outputting xml in a web page/server setting for java?

Kind Regards,

Mitch.
Rhino - 19 Jan 2006 20:31 GMT
>I am a university student using a program that requires updated xml
> from a webpage (well, a webserver).  Now i know this can be done with
[quoted text clipped - 4 lines]
> So my question is are there any tools / apis that are good at
> outputting xml in a web page/server setting for java?

Sorry, your question and your subject seem to be at odds with one another.

What format are you reading and what format are you writing?

If you're simply looking to generate HTML within a Java program, as your
subject suggests, here is  a simple Java class that does exactly that:

================================================================
public class MyHtmlWriter {

   public MyHtmlWriter() {
   }

   /**
    * Writes the file.
    *
    * @param outfile the name of the file that will be written by this
class.
    */
   public void writeFile(String outfile) {

       /* Identify the output file. */
       String strOutfile = OUTPUT_FILE_PATH + "my.html";

       /* Delete the old version of the output file. */
       File outfile = new File(strOutfile);
       outfile.delete();

       /* Open the output file. */
       FileOutputStream fos = null;
       OutputStreamWriter osw = null;
       PrintWriter pw = null;

       try {
           fos = new FileOutputStream(strOutfile, true);
       } catch (FileNotFoundException excp) {
           System.err.println("FileNotFoundException on file " + strOutfile
+ ". Error: " + excp);
           excp.printStackTrace();
           return;
       }

       osw = new OutputStreamWriter(fos);
       pw = new PrintWriter(new BufferedWriter(osw), true);

       printWriter.println("<html>");
       printWriter.println("<body>");
       printWriter.println("<p>This is my document.</p>");
       printWriter.println("</body>");
       printWriter.println("</html>");

       printWriter.flush(); /* write out any unwritten lines */
       printWriter.close(); /* close the file */

       /* Tell the user where to find the report. */
       System.out.println("The output was written to file " + outfile +
".");

       return;
}
}

================================================================

If that is not what you're trying to do, please clarify your requirements.

Java is certainly capable of reading and writing XML if that is a concern
for you.

It is also possible to use something called XSL to convert XML documents to
HTML. XSL could involve Java but doesn't have to; I've got some XML docs
(log files) that I convert to HTML without involving Java at all: there is
an XSL file that describes how the XML is supposed to be converted to HTML,
then I run a trivial script to do the actual conversion, supplying the name
of the XML file and the name of the XSL file. The XSL is capable of some
reasonable smarts so it can choose particular records and ignore others,
format some records differently than others, etc.

It's probably not a good idea to use Java for everything, just because it is
possible. You can use a wrench to hammer something but sometimes a hammer is
a better choice. :-)

Rhino
Stefan Ram - 20 Jan 2006 05:38 GMT
>If you're simply looking to generate HTML within a Java program, as your
>subject suggests, here is  a simple Java class that does exactly that:

 Today, to answer a question in another newsgroup, I wrote
 an example to show how to do basic HTML-templating with a
 small class using only standard J2SE operations.

public class Main
{

 /* Assume, you have a basket of shopping items */

 final static java.lang.String basket[] =
 { "bread <will cost only $2 tomorrow>",
   "butter",
   "cheese" };

 /* And a template that is not restricted to a fixed
    number of shopping items */

 final static java.lang.String template =
 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n" +
 "  \"http://www.w3.org/TR/html4/strict.dtd\">\n" +
 "<HTML>\n" +
 "  <HEAD>\n" +
 "    <TITLE>Warenkorb</TITLE>\n" +
 "  </HEAD>\n" +
 "  <BODY>\n" +
 "    <OL>\n" +
 "$ware[[      <LI>$name\n]]" +
 "    </OL>\n" +
 "  </BODY>\n" +
 "</HTML>\n" +
 "\n";

 /* notice that the template "      <LI>$name\n" is given
   for every $ware, implying a loop */

 public static java.lang.String quoteCharacters
 ( final java.lang.String text )
 { final java.lang.StringBuilder stringBuilder =
   new java.lang.StringBuilder( "" );
   for( int i = 0; i < text.length(); )
   { int code = text.codePointAt( i ); i += Character.charCount( code );
     if( code < ' ' || code > '~' || '<' == code ||
       '>' == code || '&' == code || '\'' == code || '\"' == code )
     { stringBuilder.append( "&#" );
       stringBuilder.append( code );
       stringBuilder.append( ";" ); }
     else
     { stringBuilder.appendCodePoint( code ); }}
    return stringBuilder.toString(); }

 public static java.lang.String wareI
 ( final java.lang.String template, final int i )
 { return java.util.regex.Matcher.quoteReplacement
   ( template.
     replaceAll( "\\$name", java.util.regex.Matcher.quoteReplacement
       ( quoteCharacters( basket[ i ] )))); }

 public static java.lang.String wares
 ( final java.lang.String template )
 { final java.lang.StringBuilder stringBuilder =
   new java.lang.StringBuilder();
   for( int i = 0; i < basket.length; ++i )
   { stringBuilder.append( "" + wareI( template, i )); }
   return stringBuilder.toString(); }

 public static java.lang.String ware
 ( final java.lang.String template )
 { java.util.regex.Pattern pattern =
   java.util.regex.Pattern.compile( "(\\$ware)\\[\\[([^\001]*?)\\]\\]" );
   java.util.regex.Matcher matcher = pattern.matcher( template );
   java.lang.StringBuffer stringBuffer = new java.lang.StringBuffer();
   while( matcher.find() )
   { final java.lang.String string = matcher.group( 2 );
     matcher.appendReplacement
     ( stringBuffer, wares( string )); }
   matcher.appendTail( stringBuffer );
   return stringBuffer.toString(); }

 public static void main( final java.lang.String[] args )
 { java.lang.String template;
   template = ware( Main.template );
   java.lang.System.out.println( template );
   java.lang.System.out.println( basket ); }}

 The output is:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
 "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
 <HEAD>
   <TITLE>Warenkorb</TITLE>
 </HEAD>
 <BODY>
   <OL>
     <LI>bread &#60;will cost only $2 tomorrow&#62;
     <LI>butter
     <LI>cheese
   </OL>
 </BODY>
</HTML>

   
Hal Rosser - 19 Jan 2006 21:54 GMT
> So my question is are there any tools / apis that are good at
> outputting xml in a web page/server setting for java?

Try JSP and Servlets with Apache Server
This book by Murach will lead you by the hand
http://www.murach.com/books/jsps/index.htm
John C. Bollinger - 20 Jan 2006 04:55 GMT
[[attribution dropped] wrote:]
>>So my question is are there any tools / apis that are good at
>>outputting xml in a web page/server setting for java?
>
> Try JSP and Servlets with Apache Server
> This book by Murach will lead you by the hand
> http://www.murach.com/books/jsps/index.htm

To amplify on that a bit, Java Servlets are the fundamental Java
technology for serving web requests.  Java Server Pages (JSP) is a web
scripting framework that operates on top of servlets and is tightly
integrated.  There are other frameworks that run either alongside or on
top of these but chances are good that you will not need them.

If you will be serving XML documents that are mostly boilerplate but
have some dynamic parts, then JSP is a great choice.  If you will be
serving highly dynamic documents then you probably want a pure servlet.
 In the middle, the choice is a little less clear.

Signature

John Bollinger
jobollin@indiana.edu



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.