SAXParseException while parsing the data

Posted on 2008-10-10
Last Modified: 2013-11-23
Hi All,

I'm trying to write parse XML using JAXP 1.1. I've given input to parser as shown in below code.

SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
parser.parse(new File(args[0], handler);

this worked for me. But I've tried to pass reference to the overloaded parse() method of SAXParser class, it throwed the following exception

org.xml.sax.SAXParseException: Content is not allowed in trailing section.
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(
at javax.xml.parsers.SAXParser.parse(
at CMMCompareHelper.main(

I'm using JAXP 1.1 with J2SE 1..4.2. My requirement is that I get the XML as a String and I've tried to convert it to and passed to the SAXParser. It din't work. Code I've written to do this is shown below:

FileInputStream in = new FileInputStream(new File(args[0]));
byte[] buf = new byte[512];
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
while( != -1){
InputStream newCMMStream = new ByteArrayInputStream(byteOut.toByteArray());                  
SAXParser parser = factory.newSAXParser();                  
parser.parse(newCMMStream, handler);

Am I doing something wrong?
Thanks in advance for any help in this regard.

Question by:ysatyakrishna

Expert Comment

ID: 22700204
Try using following statement to convert your xml string directly into InputStream (without using FileInputStream)  :

InputStream newCMMStream = new ByteArrayInputStream(xmlString.getBytes("UTF-8"));


Author Comment

ID: 22718952

I got it worked by getting the input from String instead of File.

Thank you,

LVL 86

Expert Comment

ID: 22879156
while( != -1){

You write the whole of the buffer. That would only work in the very unlikely eventuality that inputlength % buf.length == 0. Otherwise you're going to write garbage at the end

Accepted Solution

Computer101 earned 0 total points
ID: 23180300
PAQed with points refunded (500)

EE Admin

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 52
session migration servlets 2 42
couple of eclipse 5 46
Opening PDF on button click and fill new document 2 47
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question