Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > org.xml.sax.SAXParseException: Premature end of file

Reply
Thread Tools

org.xml.sax.SAXParseException: Premature end of file

 
 
laredotornado
Guest
Posts: n/a
 
      10-20-2011
Hi,

I'm using Java 6. I'm trying to parse some well-formed (or at least
that's what I thought) XML but am getting a "SAXParseException:
Premature end of file" exception (below). The code I'm using is

xml = xml.trim();
final InputStream inputStream = new
ByteArrayInputStream(xml.getBytes());
final DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
final DocumentBuilder db = dbf.newDocumentBuilder();
final Document doc = db.parse(xmlInputStream);

The block I'm trying to parse is below. Can anyone see how it is mal-
formed? Barring that, do you know a better way of parsing the string?

<div class="">
<label
for="id417474726962757465205b6e616d653d6e616d652c2 076616c75653d4c697374696e672052756c652c20706172656 e744e6f64653d546578744e6f6465205b746578743d6e756c6 c2c20444e6f64653d444e6f6465205b69643d305d5d5d">400 </
label><br/>
<input
id="id417474726962757465205b6e616d653d6e616d652c20 76616c75653d4c697374696e672052756c652c20706172656e 744e6f64653d546578744e6f6465205b746578743d6e756c6c 2c20444e6f64653d444e6f6465205b69643d305d5d5d"
name="id417474726962757465205b6e616d653d6e616d652c 2076616c75653d4c697374696e672052756c652c2070617265 6e744e6f64653d546578744e6f6465205b746578743d6e756c 6c2c20444e6f64653d444e6f6465205b69643d305d5d5d"
type="text" value="
15 months of jun, jul, aug, sep, oct
" style="width:400px;" />
</div>

Thanks, - Dave

ps - The error ...

org.xml.sax.SAXParseException: Premature end of file.
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(U nknown Source)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBu ilder.java:124)
at com.myco.clearing.commons.xmlparser.XMLParser.pars e(XMLParser.java:
53)
at com.myco.clearing.commons.xmlparser.XMLParser.pars e(XMLParser.java:
35)
at com.myco.clearing.commons.xmlparser.XMLParser.pars e(XMLParser.java:
80)
at com.myco.clearing.commons.xmlparser.XMLParser.pars e(XMLParser.java:
35)
at
com.myco.pplus2.util.XmlToHtmlServiceTest.testInpu t(XmlToHtmlServiceTest.java:
52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:
39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:
25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod
$1.runReflectiveCall(FrameworkMethod.java:44)
at
org.junit.internal.runners.model.ReflectiveCallabl e.run(ReflectiveCallable.java:
15)
at
org.junit.runners.model.FrameworkMethod.invokeExpl osively(FrameworkMethod.java:
41)
at
org.junit.internal.runners.statements.InvokeMethod .evaluate(InvokeMethod.java:
20)
at
org.junit.internal.runners.statements.RunBefores.e valuate(RunBefores.java:
2
at
org.junit.runners.BlockJUnit4ClassRunner.runChild( BlockJUnit4ClassRunner.java:
76)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild( BlockJUnit4ClassRunner.java:
50)
at org.junit.runners.ParentRunner$3.run(ParentRunner. java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRu nner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentR unner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRu nner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRu nner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.ja va:236)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestR eference.run(JUnit4TestReference.java:
50)
at
org.eclipse.jdt.internal.junit.runner.TestExecutio n.run(TestExecution.java:
3
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:
467)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:
683)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:
390)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:
197)
 
Reply With Quote
 
 
 
 
markspace
Guest
Posts: n/a
 
      10-20-2011
On 10/20/2011 8:41 AM, laredotornado wrote:

> I'm using Java 6. I'm trying to parse some well-formed (or at least
> that's what I thought) XML but am getting a "SAXParseException:



It worked for me.


> Premature end of file" exception (below). The code I'm using is



No, this is not the code. There's a syntax error: inputStream and
xmlInputStream are being confused here, xmlInputStream is not declared.


>
> xml = xml.trim();
> final InputStream inputStream = new
> ByteArrayInputStream(xml.getBytes());
> final DocumentBuilderFactory dbf =
> DocumentBuilderFactory.newInstance();
> final DocumentBuilder db = dbf.newDocumentBuilder();
> final Document doc = db.parse(xmlInputStream);
>
> The block I'm trying to parse is below. Can anyone see how it is mal-
> formed? Barring that, do you know a better way of parsing the string?



Yes, there's a syntax error in the XML. Construct a proper SSCCE to see it.



 
Reply With Quote
 
 
 
 
laredotornado
Guest
Posts: n/a
 
      10-20-2011
On Oct 20, 12:04*pm, markspace <-@.> wrote:
> On 10/20/2011 8:41 AM, laredotornado wrote:
>
> > I'm using Java 6. *I'm trying to parse some well-formed (or at least
> > that's what I thought) XML but am getting a "SAXParseException:

>
> It worked for me.
>
> > Premature end of file" exception (below). *The code I'm using is

>
> No, this is not the code. *There's a syntax error: inputStream and
> xmlInputStream are being confused here, xmlInputStream is not declared.
>
>
>
> > * * * * * *xml = xml.trim();
> > * * * * * *final InputStream inputStream = new
> > ByteArrayInputStream(xml.getBytes());
> > * * * * * *final DocumentBuilderFactory dbf =
> > DocumentBuilderFactory.newInstance();
> > * * * * * *final DocumentBuilder db = dbf.newDocumentBuilder();
> > * * * * * *final Document doc = db.parse(xmlInputStream);

>
> > The block I'm trying to parse is below. *Can anyone see how it is mal-
> > formed? *Barring that, do you know a better way of parsing the string?

>
> Yes, there's a syntax error in the XML. *Construct a proper SSCCE to see it.


You mean a syntax error in the Java? Ok, here it is ...

public static com.myco.clearing.commons.xml.Node parse(String xml)
throws
ParserConfigurationException, SAXException, IOException {
xml = xml.trim();
return parse(new ByteArrayInputStream(xml.getBytes()));
}

/**
*
* @param xml
* @return Parsed xml
* @throws XMLParseException
* @throws ParserConfigurationException
* @throws IOException
* @throws SAXException
*/
public static com.myco.clearing.commons.xml.Node parse(final
InputStream xmlInputStream)
throws ParserConfigurationException,
SAXException, IOException {

final DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
final DocumentBuilder db = dbf.newDocumentBuilder();
final Document doc = db.parse(xmlInputStream);
doc.getDocumentElement().normalize();

If you mean the XML, what is the XML syntax error or what tool can I
use to see it? - Dave
 
Reply With Quote
 
Daniele Futtorovic
Guest
Posts: n/a
 
      10-20-2011
On 20/10/2011 17:41, laredotornado allegedly wrote:
> xml = xml.trim();
> final InputStream inputStream = new
> ByteArrayInputStream(xml.getBytes());
> final DocumentBuilderFactory dbf =
> DocumentBuilderFactory.newInstance();
> final DocumentBuilder db = dbf.newDocumentBuilder();
> final Document doc = db.parse(xmlInputStream);


> final InputStream inputStream = new
> ByteArrayInputStream(xml.getBytes());


Don't do this.

Instead:

db.parse( new InputSource( new StringReader(xml) ) );

--
DF.
Determinism trumps correctness.
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      10-20-2011
DO NOT POST CODE USING TAB AS AN INDENTATION CHARACTER!

Bad!

laredotornado wrote:
> You mean a syntax error in the Java? Ok, here it is ...
>
> public static com.myco.clearing.commons.xml.Node parse(String xml)
> throws
> ParserConfigurationException, SAXException, IOException {
> xml = xml.trim();
> return parse(new ByteArrayInputStream(xml.getBytes()));


Encoding?

> }
>
> /**
> *
> * @param xml


Where is 'xml'?

> * @return Parsed xml


What is the definition of 'Parsed'?

> * @throws XMLParseException


No, it doesn't.

> * @throws ParserConfigurationException
> * @throws IOException
> * @throws SAXException
> */
> public static com.myco.clearing.commons.xml.Node parse(final
> InputStream xmlInputStream)
> throws ParserConfigurationException,
> SAXException, IOException {
>
> final DocumentBuilderFactory dbf =
> DocumentBuilderFactory.newInstance();
> final DocumentBuilder db = dbf.newDocumentBuilder();
> final Document doc = db.parse(xmlInputStream);
> doc.getDocumentElement().normalize();
>
> If you mean the XML, what is the XML syntax error or what tool can I
> use to see it?


Is there a newline at the end of your XML?

GIYF:
<http://www.w3schools.com/xml/xml_validator.asp>

--
Lew

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
'Premature end of script headers' error when starting Apache inboot time Greeshma Python 1 02-27-2004 08:19 AM
'Premature end of script headers' error when starting Apache inboot time Greeshma Python 0 02-25-2004 04:44 AM
Premature end of script headers Wayne Deleersnyder Perl 1 11-21-2003 01:00 PM
premature end of headers, code and output looks fine nomadx Perl 1 09-19-2003 04:01 PM
org.xml.sax.SAXEception: Premature end of file Angela Robert XML 0 06-27-2003 09:27 PM



Advertisments