You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Alistair Young <al...@smo.uhi.ac.uk> on 2004/12/14 16:26:48 UTC

parsing an InputStream from HTTP

Is there any way in Xerces/JAXP to parse an HTTP stream? I have an XML 
doc as POST data, received via HTTP. Create an InputStream for the 
bytes and pass it to DocumentBuilder.parse(InputStream). It always 
throws a premature end of file exception. The only way that works is to 
write it to file first, then parse it.
thanks,
Alistair


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


Re: parsing an InputStream from HTTP

Posted by Alistair Young <al...@smo.uhi.ac.uk>.
ah! if I wasn't so daft, I'd get some work done. 
request.getInputStream() returns the same object each time. I'd already 
called it and read from it without reset()ing it! Hence the EOF, 
Perfectly logical.
this now works:

doc = builder.parse(new InputSource(request.getInputStream()));

thanks,
Alistair

On 15 Dec 2004, at 10:53, Alistair Young wrote:

> I tried what Robert suggested but using JAXP but get the same 
> SAXParseException - Premature end of file.
> If I just use request.getInputStream() and write the bytes to file and 
> then parse that it's fine. Parsing straight from the request doesn't 
> seem to work for me though.
>
> DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
> factory.setNamespaceAware(true);
> try {
>   DocumentBuilder builder = factory.newDocumentBuilder();
>   Document doc = builder.parse(new 
> InputSource(request.getInputStream()));
> }
>
> thanks,
> Alistair
>
>
> On 15 Dec 2004, at 09:28, Stanimir Stamenkov wrote:
>
>> /Alistair Young/:
>>
>>> Is there any way in Xerces/JAXP to parse an HTTP stream? I have an 
>>> XML doc as POST data, received via HTTP. Create an InputStream for 
>>> the bytes and pass it to DocumentBuilder.parse(InputStream). It 
>>> always throws a premature end of file exception. The only way that 
>>> works is to write it to file first, then parse it.
>>
>> Show some code of your case.
>>
>>     DocumentBuilderFactory builderFactory;
>>     ...
>>
>>     (ServletRequest req, ServletResponse resp)
>>
>>     DocumentBuilder builder = builderFactory.newDocumentBuilder();
>>     Document doc = builder.parse(req.getInputStream());
>>
>> Does it work?
>>
>> While it is not typically the case the ServletRequest.getParameter() 
>> documentation describes it could interfere with the 
>> ServletRequest.getInputStream() when the parameters are served with 
>> the request body (as it is the case with HTTP POST).
>>
>> -- 
>> Stanimir
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
>> For additional commands, e-mail: xerces-j-user-help@xml.apache.org
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


Re: parsing an InputStream from HTTP

Posted by Alistair Young <al...@smo.uhi.ac.uk>.
I tried what Robert suggested but using JAXP but get the same 
SAXParseException - Premature end of file.
If I just use request.getInputStream() and write the bytes to file and 
then parse that it's fine. Parsing straight from the request doesn't 
seem to work for me though.

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
try {
   DocumentBuilder builder = factory.newDocumentBuilder();
   Document doc = builder.parse(new 
InputSource(request.getInputStream()));
}

thanks,
Alistair


On 15 Dec 2004, at 09:28, Stanimir Stamenkov wrote:

> /Alistair Young/:
>
>> Is there any way in Xerces/JAXP to parse an HTTP stream? I have an 
>> XML doc as POST data, received via HTTP. Create an InputStream for 
>> the bytes and pass it to DocumentBuilder.parse(InputStream). It 
>> always throws a premature end of file exception. The only way that 
>> works is to write it to file first, then parse it.
>
> Show some code of your case.
>
>     DocumentBuilderFactory builderFactory;
>     ...
>
>     (ServletRequest req, ServletResponse resp)
>
>     DocumentBuilder builder = builderFactory.newDocumentBuilder();
>     Document doc = builder.parse(req.getInputStream());
>
> Does it work?
>
> While it is not typically the case the ServletRequest.getParameter() 
> documentation describes it could interfere with the 
> ServletRequest.getInputStream() when the parameters are served with 
> the request body (as it is the case with HTTP POST).
>
> -- 
> Stanimir
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


Re: parsing an InputStream from HTTP

Posted by Stanimir Stamenkov <st...@myrealbox.com>.
/Alistair Young/:

> Is there any way in Xerces/JAXP to parse an HTTP stream? I have an XML 
> doc as POST data, received via HTTP. Create an InputStream for the bytes 
> and pass it to DocumentBuilder.parse(InputStream). It always throws a 
> premature end of file exception. The only way that works is to write it 
> to file first, then parse it.

Show some code of your case.

     DocumentBuilderFactory builderFactory;
     ...

     (ServletRequest req, ServletResponse resp)

     DocumentBuilder builder = builderFactory.newDocumentBuilder();
     Document doc = builder.parse(req.getInputStream());

Does it work?

While it is not typically the case the ServletRequest.getParameter() 
documentation describes it could interfere with the 
ServletRequest.getInputStream() when the parameters are served with 
the request body (as it is the case with HTTP POST).

-- 
Stanimir


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


Re: parsing an InputStream from HTTP

Posted by Jon Schewe <jp...@mtu.net>.
On Tue, 2004-12-14 at 12:56, Robert Koberg wrote:

> Jon Schewe wrote:
> > On Tue, 2004-12-14 at 11:16, Robert Koberg wrote:
> > 
> >>/Alistair Young wrote:
> >>> Is there any way in Xerces/JAXP to parse an HTTP stream? I have an XML 
> >>> doc as POST data, received via HTTP. Create an InputStream for the bytes 
> >>> and pass it to DocumentBuilder.parse(InputStream). It always throws a 
> >>> premature end of file exception. The only way that works is to write it 
> >>> to file first, then parse it.
> >>
> >>
> >>Hi,
> >>
> >>
> >>InputStream is = request.getInputStream();
> >>DOMParser parser = new DOMParser();
> >>/
> >>
> > But where is DOMParser?  I'm looking at the 2.6.0 API documentation and 
> > see no such class.
> 
> 
> http://xml.apache.org/xerces2-j/javadocs/xerces2/org/apache/xerces/parsers/DOMParser.html


Thank you.  I wasn't looking at the implementation API.



________________________________________________________________________
Jon Schewe | http://mtu.net/~jpschewe
GPG signature at http://mtu.net/~jpschewe/gpg.sig.html
For I am convinced that neither death nor life, neither angels 
nor demons, neither the present nor the future, nor any 
powers, neither height nor depth, nor anything else in all 
creation, will be able to separate us from the love of God that 
is in Christ Jesus our Lord. - Romans 8:38-39


Re: parsing an InputStream from HTTP

Posted by Robert Koberg <ro...@koberg.com>.
Jon Schewe wrote:
> On Tue, 2004-12-14 at 11:16, Robert Koberg wrote:
> 
>>/Alistair Young wrote:
>>> Is there any way in Xerces/JAXP to parse an HTTP stream? I have an XML 
>>> doc as POST data, received via HTTP. Create an InputStream for the bytes 
>>> and pass it to DocumentBuilder.parse(InputStream). It always throws a 
>>> premature end of file exception. The only way that works is to write it 
>>> to file first, then parse it.
>>
>>
>>Hi,
>>
>>
>>InputStream is = request.getInputStream();
>>DOMParser parser = new DOMParser();
>>/
>>
> But where is DOMParser?  I'm looking at the 2.6.0 API documentation and 
> see no such class.


http://xml.apache.org/xerces2-j/javadocs/xerces2/org/apache/xerces/parsers/DOMParser.html


> 
> ------------------------------------------------------------------------
> 
> Jon Schewe | _http://mtu.net/~jpschewe_
> GPG signature at _http://mtu.net/~jpschewe/gpg.sig.html_
> For I am convinced that neither death nor life, neither angels
> nor demons, neither the present nor the future, nor any
> powers, neither height nor depth, nor anything else in all
> creation, will be able to separate us from the love of God that
> is in Christ Jesus our Lord. - Romans 8:38-39
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


Re: parsing an InputStream from HTTP

Posted by Jon Schewe <jp...@mtu.net>.
On Tue, 2004-12-14 at 11:16, Robert Koberg wrote:

> Alistair Young wrote:
> > Is there any way in Xerces/JAXP to parse an HTTP stream? I have an XML 
> > doc as POST data, received via HTTP. Create an InputStream for the bytes 
> > and pass it to DocumentBuilder.parse(InputStream). It always throws a 
> > premature end of file exception. The only way that works is to write it 
> > to file first, then parse it.
> 
> 
> Hi,
> 
> 
> InputStream is = request.getInputStream();
> DOMParser parser = new DOMParser();
> 

But where is DOMParser?  I'm looking at the 2.6.0 API documentation and
see no such class.



________________________________________________________________________
Jon Schewe | http://mtu.net/~jpschewe
GPG signature at http://mtu.net/~jpschewe/gpg.sig.html
For I am convinced that neither death nor life, neither angels 
nor demons, neither the present nor the future, nor any 
powers, neither height nor depth, nor anything else in all 
creation, will be able to separate us from the love of God that 
is in Christ Jesus our Lord. - Romans 8:38-39


Re: parsing an InputStream from HTTP

Posted by Robert Koberg <ro...@koberg.com>.
Alistair Young wrote:
> Is there any way in Xerces/JAXP to parse an HTTP stream? I have an XML 
> doc as POST data, received via HTTP. Create an InputStream for the bytes 
> and pass it to DocumentBuilder.parse(InputStream). It always throws a 
> premature end of file exception. The only way that works is to write it 
> to file first, then parse it.


Hi,


InputStream is = request.getInputStream();
DOMParser parser = new DOMParser();

try {
   InputSource is = new InputSource(is);
   parser.parse(is);
} catch (Exception e) {
...
}

Document doc = parser.getDocument();


best,
-Rob


> thanks,
> Alistair
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org