Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SAXParseException while parsing the data

Posted on 2008-10-10
5
Medium Priority
?
226 Views
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 java.io.InputStream 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(SAXParser.java:345)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:143)
at CMMCompareHelper.main(CMMCompareHelper.java:234)


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 java.io.ByteArrayInputStream 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(in.read(buf) != -1){
    byteOut.write(buf);                        
}
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.

Satya
0
Comment
Question by:ysatyakrishna
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 5

Expert Comment

by:muktajindal
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"));

0
 

Author Comment

by:ysatyakrishna
ID: 22718952
Hi,

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

Thank you,

Satya
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 22879156
>>
while(in.read(buf) != -1){
    byteOut.write(buf);                        
}
>>

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
0
 
LVL 1

Accepted Solution

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

Computer101
EE Admin
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

636 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