• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating HTML with XML using SAX

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Below is a program which i wrote to convert an XML file to a HTML file using (SAX)Simple API for XML.
But i keep getting the error "Error setting up XML Processor" whenever i run it and the HTML file produced is empty. Kindly run through and advise as i am very lost on this error.

Regards
Shilong

import org.xml.sax.*;
import java.util.*;
import java.io.*;


public class MarksProcessor extends HandlerBase{

private PrintWriter pw = null;

public MarksProcessor(String file){
try{
pw = new PrintWriter(new FileWriter("Classmarks.html"));
}catch(IOException e){
System.out.println("File IO Error:" + e.getMessage());
}

try{
Class loadedClass = Class.forName("com.ibm.xml.parser.SAXDriver");
Parser xParser = (Parser)loadedClass.newInstance();
xParser.setDocumentHandler(this);
xParser.parse(file);
}catch(Exception e){
System.out.println("Error setting up XML Processor");
}
}

public static void main(String[] args){
MarksProcessor MarksProcessor1 = new MarksProcessor("Classmarks.xml");
}

public void error(SAXParseException se){
System.out.println("Error !! Problem with XML" + se.getMessage());
}

public void warning(SAXParseException se){
System.out.println("Warning ! Problem with XML" + se.getMessage());
}

public void startDocument() throws SAXException{
pw.println("<HTML>");
pw.println("<HEAD>");
pw.println("<TITLE>Student List</TITLE>");
pw.println("</HEAD>");
pw.println("<BODY>");
}

public void startElement(String elementName, AttributeList al) throws SAXException{
if(elementName.equalsIgnoreCase("STUDENTLIST")){
pw.println("<TABLE border='1'>");
pw.println("<TR>");
pw.println("<TD>STUDENTMARK</TD>");
pw.println("<TD>ASSIGNMENTNO</TD>");
pw.println("<TD>MARK</TD>");
pw.println("</TR>");
}else if(elementName.equalsIgnoreCase("STUDENTMARK")){
pw.println("<TR>");
}else{
pw.println("<TD>");
}
}

public void endElement(String elementName) throws SAXException{
if(elementName.equalsIgnoreCase("STUDENTLIST")){
pw.println("</TABLE>");
}else if(elementName.equalsIgnoreCase("STUDENTMARK")){
pw.println("</TR>");
}else{
pw.println("</TD>");
}
}

public void endDocument() throws SAXException{
pw.println("</BODY>");
pw.println("</HTML>");
pw.close();
}

public void characters(char[] chars, int start, int length)throws SAXException{
String stringRead = new String(chars, 1, length - 2);
System.out.println("String read is: " + stringRead );
}
}
 
author and deputy
Posts: 3150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The error is most likely that the parser is not able to load the class com.ibm.xml.SAXDriver..
Are you sure, you had setted-up the classpast to the jar or package ?

Also it is better to print the error fully by appending "e" like..
try{
Class loadedClass = Class.forName("com.ibm.xml.parser.SAXDriver");
Parser xParser = (Parser)loadedClass.newInstance();
xParser.setDocumentHandler(this);
xParser.parse(file);
}catch(Exception e){
System.out.println("Error setting up XML Processor: " + e);
}
}
 
Balaji Loganathan
author and deputy
Posts: 3150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to XML and other technologies forum.....
 
Shilong Zheng
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I tried that and received the error "Classmarks.xml (The system cannot find the file specified)". So where should i place the XML file to make it work. I am still using JBuilder 4.
Kindly advise on this small error, thanks a million...

Regards
Shilong
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
xParser.parse(file); // where file is a String
Probably you are not giving a complete path to the file. Using just the file name depends on the "current" file directory.
I suggest that you use the complete path and do something like:

File f = new File( file );
if( !f.exists() ){
System.out.println("Can't find " + file );
return;
}
Bill
 
Shilong Zheng
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi William,

After trying, i just get the error "Can't find Classmarks.xml". Any idea how to get the program to find my XML file??? Thanks


Regards
Shilong
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try

This should tell you the full path of the missing file.
 
Shilong Zheng
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The file class in my jBuilder 4 does not have the method getAbsolutePath() what can i get the method??? Sorry for all the ignorance, really appreciate your expertise.


Regards
Shilong
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The java.io.File class has had the getAbsolutePath method since Java 1 so something is really odd here.
I feel sure that your problem is that you are not giving a complete path to the Classmarks.xml file, or that the name is not exactly right.
Bill
 
Weeds: because mother nature refuses to be your personal bitch. But this tiny ad is willing:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic