ysatyakrishna
asked on
SAXParseException while parsing the data
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.newInstan ce();
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.SAXParseExcept ion: Content is not allowed in trailing section.
at org.apache.xerces.parsers. AbstractSA XParser.pa rse(Unknow n Source)
at javax.xml.parsers.SAXParse r.parse(SA XParser.ja va:345)
at javax.xml.parsers.SAXParse r.parse(SA XParser.ja va:143)
at CMMCompareHelper.main(CMMC ompareHelp er.java:23 4)
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.ByteArrayInputStre am 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(byteO ut.toByteA rray());
SAXParser parser = factory.newSAXParser();
parser.parse(newCMMStream, handler);
Am I doing something wrong?
Thanks in advance for any help in this regard.
Satya
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.newInstan
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.SAXParseExcept
at org.apache.xerces.parsers.
at javax.xml.parsers.SAXParse
at javax.xml.parsers.SAXParse
at CMMCompareHelper.main(CMMC
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.ByteArrayInputStre
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(byteO
SAXParser parser = factory.newSAXParser();
parser.parse(newCMMStream,
Am I doing something wrong?
Thanks in advance for any help in this regard.
Satya
ASKER
Hi,
I got it worked by getting the input from String instead of File.
Thank you,
Satya
I got it worked by getting the input from String instead of File.
Thank you,
Satya
>>
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
InputStream newCMMStream = new ByteArrayInputStream(xmlSt