[jdom-interest] SAXBuilder.build(java.io.Reader characterStream) Error
Kevin Baynes
kbaynes at renex.com
Fri Feb 16 12:22:58 PST 2001
I've run into a problem building a JDOM document by passing a Reader into
the SAXBuilder (today's snapshot).
In the sample application included below, I can pass two direct subclasses
of java.io.Reader into the SAXBuilder.build(java.io.Reader characterStream)
method and get success - BufferedReader and InputStreamReader.
However, when I try to pass the FileReader subclass of InputStreamReader, I
get a failure.
I even tried upcasting the FileReader to InputStreamReader and Reader, which
both fail.
I'm getting an error that appears to be similar to the EOF Socket error:
org.jdom.JDOMException: The root element is required in a well-formed
document.: Error on line 1: The root element is required in a well-formed
document.
It seems to me that if I can pass a two subclasses of Reader and get
success, that all the subclasses should work. (all subclasses should have
all features of superclass).
Am I wrong in this thought? Can you see some error I am making?
You can download the following files to check me:
ftp://ftpGuest:ftpGuest@hemlock.renex.com/RequestParser2.java
ftp://ftpGuest:ftpGuest@hemlock.renex.com/RequestParser2.class
ftp://ftpGuest:ftpGuest@hemlock.renex.com/trace.txt (pass this file to
RequestParser)
Pass through the file to parse as command line args: java RequestParser2
trace.txt
Thanks!
-Kevin Baynes
kbaynes at renex.com
Here is the file that will generate the errors:
import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import java.util.*;
public class RequestParser2 {
public static void main(String[] args) {
//if ( "Reader".equalsIgnoreCase(args[1]) )
try {
File theFile = new File(args[0]);
FileReader file = new FileReader(theFile);
BufferedReader buff = new BufferedReader(file);
FileInputStream fis = new FileInputStream(new File(args[0]));
InputStreamReader isr = new InputStreamReader(new FileInputStream(new
File(args[0])));
SAXBuilder builder = new SAXBuilder();
XMLOutputter fmt = new XMLOutputter();
Document doc;
int i;
try {
// build and output with FileInputStream
System.out.println("\n\n******************\nbuild and output with
FileInputStream. Ready? (hit 'Enter')");
System.in.read();System.in.read();
doc = builder.build(fis);
fmt.output(doc,System.out);
} catch (Exception e) {
e.printStackTrace();
}
try {
// build and output with BufferedReader
System.out.println("\n\n******************\nbuild and output with
BufferedReader. Ready? (hit 'Enter')");
System.in.read();System.in.read();
doc = builder.build(buff);
fmt.output(doc,System.out);
} catch (Exception e) {
e.printStackTrace();
}
try {
// build and output with FileReader
System.out.println("\n\n******************\nbuild and output with
FileReader. Ready? (hit 'Enter')");
System.in.read();System.in.read();
doc = builder.build( file);
fmt.output(doc,System.out);
} catch (Exception e) {
e.printStackTrace();
}
try {
// build and output with FileReader cast to Reader
System.out.println("\n\n******************\nbuild and output with
FileReader cast to Reader. Ready? (hit 'Enter')");
System.in.read();System.in.read();
doc = builder.build((java.io.Reader)file);
fmt.output(doc,System.out);
} catch (Exception e) {
e.printStackTrace();
}
try {
// build and output with FileReader cast to InputStreamReader
System.out.println("\n\n******************\nbuild and output with
FileReader cast to InputStreamReader. Ready? (hit 'Enter')");
System.in.read();System.in.read();
doc = builder.build((java.io.InputStreamReader)file);
fmt.output(doc,System.out);
} catch (Exception e) {
e.printStackTrace();
}
try {
// build and output with InputStreamReader
System.out.println("\n\n******************\nbuild and output with
InputStreamReader. Ready? (hit 'Enter')");
System.in.read();System.in.read();
doc = builder.build( isr );
fmt.output(doc,System.out);
} catch (Exception e) {
e.printStackTrace();
}
try {
// build and output with File
System.out.println("\n\n******************\nbuild and output with File.
Ready? (hit 'Enter')");
System.in.read();System.in.read();
doc = builder.build(theFile);
fmt.output(doc,System.out);
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
} // end RequestParser2 class
Here is the file being parsed:
<?xml version="1.0"
encoding="UTF-8"?><WMREQUEST><ACTION>adminLogin</ACTION><PAYLOAD><LOGINID>ke
vin</LOGINID><PWORD>kegvin</PWORD></PAYLOAD></WMREQUEST>
More information about the jdom-interest
mailing list