Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

Xml doc = dom.parseString(request.getReader());

Thread view: 
gert - 23 Sep 2007 19:20 GMT
Can anybody tell me howto parse a request.getReader() buffer into a
xml dom object where you can do things like
object.getElementTag("mytag") for example.
Daniel Pitts - 23 Sep 2007 19:29 GMT
> Can anybody tell me howto parse a request.getReader() buffer into a
> xml dom object where you can do things like
> object.getElementTag("mytag") for example.

Look at the DocumentBuilderFactory, DocumentBuilder, and Document
classes.  You won't be disappointed.
gert - 23 Sep 2007 21:11 GMT
> > Can anybody tell me howto parse a request.getReader() buffer into a
> > xml dom object where you can do things like
> > object.getElementTag("mytag") for example.
>
> Look at the DocumentBuilderFactory, DocumentBuilder, and Document
> classes.  You won't be disappointed.

Looks promising indeed :)

http://java.sun.com/j2se/1.4.2/docs/api/javax/xml/parsers/package-summary.html
gert - 23 Sep 2007 23:39 GMT
How can i make a BufferedReader a inputstream, can i just cast it to a
input stream ?

Parse requires a inputstream or string ?

import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;

Documnet doc;
DocumentBuilder parser;
BufferedReader in = request.getReader();

doc = parser.parse(in);
gert - 24 Sep 2007 00:05 GMT
> How can i make a BufferedReader a inputstream, can i just cast it to a
> input stream ?
[quoted text clipped - 9 lines]
>
> doc = parser.parse(in);

casting doesn't work

InputStream in  = (InputStream) request.getReader();

Any idea's ?
gert - 24 Sep 2007 00:32 GMT
> > How can i make a BufferedReader a inputstream, can i just cast it to a
> > input stream ?
[quoted text clipped - 15 lines]
>
> Any idea's ?

never mind found it :)

       BufferedReader in  = request.getReader();
       while((input = in.readLine()) != null)
       {
           xmlText = xmlText + input;
           in.close();
       }
Daniel Pitts - 24 Sep 2007 00:40 GMT
> > > How can i make a BufferedReader a inputstream, can i just cast it to a
> > > input stream ?
[quoted text clipped - 24 lines]
>             in.close();
>         }

That seems quite suboptimal...
What kind of object is request?
Is there a getStream() instead of getReader()?

At the very worst, you can use InputSource instead of InputStream:

import org.xml.sax.InputSource;
...

builder.parse(new InputSource(in));
gert - 24 Sep 2007 01:46 GMT
I get a java.lang.NullPointerException when i do this ?

try {doc = parser.parse(xmlText);}
catch (SAXException ex){out.println(" <error>"+ex+"</error>\n");}

Debugger or compiler doesn't give me any warnings or anything ?
gert - 24 Sep 2007 02:05 GMT
> I get a java.lang.NullPointerException when i do this ?
>
> try {doc = parser.parse(xmlText);}
> catch (SAXException ex){out.println(" <error>"+ex+"</error>\n");}
>
> Debugger or compiler doesn't give me any warnings or anything ?

Why do i always get a 500 response ?

try {doc = parser.parse(new InputSource(in));}
catch (SAXException ex){out.println(" <error>"+ex+"</error>\n");}

deployed with moduleid = www
StandardWrapperValve[query]: PWC1406: Servlet.service() for servlet
query threw exception
java.lang.NullPointerException
       at query.doPost(query.java:37)
Daniel Pitts - 24 Sep 2007 03:31 GMT
> > I get a java.lang.NullPointerException when i do this ?
>
[quoted text clipped - 13 lines]
> java.lang.NullPointerException
>         at query.doPost(query.java:37)

What is on line 37 of query.java?

BTW, Class names (and therefor .java files) should always start with a
capitol case letter.  "Query" would then be appropriate.

Make sure that "parser" is an actual value, and not just null.  You
need to get it from the
DocumentBuilderFactory().newInstance().newDocumentBuilder()
gert - 24 Sep 2007 04:05 GMT
> > > I get a java.lang.NullPointerException when i do this ?
>
[quoted text clipped - 22 lines]
> need to get it from the
> DocumentBuilderFactory().newInstance().newDocumentBuilder()

Here is the source code.
http://appwsgi.googlecode.com/svn/trunk/java/query.java
Feel free to give allot of suggestions :)

NullPointerException refers to,
doc = parser.parse(new InputSource(in));
Daniel Pitts - 24 Sep 2007 04:34 GMT
> > > > I get a java.lang.NullPointerException when i do this ?
>
[quoted text clipped - 28 lines]
> NullPointerException refers to,
> doc = parser.parse(new InputSource(in));

Add somewhere:
parser = DocumentBuilderFactory().newInstance().newDocumentBuilder()
gert - 24 Sep 2007 13:36 GMT
> > > > > I get a java.lang.NullPointerException when i do this ?
>
[quoted text clipped - 31 lines]
> Add somewhere:
> parser = DocumentBuilderFactory().newInstance().newDocumentBuilder()

import javax.xml.parsers.*;

DocumentBuilder parser = null;
parser = DocumentBuilderFactory().newInstance().newDocumentBuilder();

I am sorry i must misunderstand something because the compiler doesn't
like it telling me it cant find the symbols.
Lew - 24 Sep 2007 13:53 GMT
Daniel Pitts wrote:
>> Add somewhere:
>> parser = DocumentBuilderFactory().newInstance().newDocumentBuilder()

> import javax.xml.parsers.*;
>
[quoted text clipped - 3 lines]
> I am sorry i must misunderstand something because the compiler doesn't
> like it telling me it cant find the symbols.

It's time for you to provide an "SSCCE" - a Simple, Self-Contained Complete
Example.  That is, a complete, simplified example that yields the error of
interest.  Complete.  Simplified.  Complete.

We need /actual/ code and /actual/ compiler messages.  Note that it is not
possible that "the compiler doesn't like it"; compilers don't have emotions.
We are unable to diagnose your issue or assist you given only anthropomorphic
approximations to the evidence.

Java note:
>> DocumentBuilder parser = null;
>> parser = DocumentBuilderFactory().newInstance().newDocumentBuilder();

Just use
 DocumentBuilder parser =
  DocumentBuilderFactory().newInstance().newDocumentBuilder();

Why initialize the variable twice?

Also, in English the word "I" is always capitalized, and use punctuation to
avoid ambiguity.  "[T]he compiler doesn't like it telling me..." means, "the
compiler doesn't like [some previously mentioned thing] to tell me ...",
whereas I suspect you intended, "The compiler doesn't like it, telling me ...".

Programming is an art of precision.

Signature

Lew

--
Lew

Andrew Thompson - 24 Sep 2007 14:11 GMT
>> > > > > I get a java.lang.NullPointerException when i do this ?
...
>> Add somewhere:
>> parser = DocumentBuilderFactory().newInstance().newDocumentBuilder()
..
>parser = DocumentBuilderFactory().newInstance().newDocumentBuilder();

"I think what Mr. Pitts* meant to say was.."
parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();

However, when code is breaking, it is best to break it
down to the simnplest units possible.  For that reason,
I would change that single line to.

DocumentBuilderFactory factory =
 DocumentBuilderFactory.newInstance();
System.out.println("factory: " + factory);
parser = factory.newDocumentBuilder();
System.out.println("parser: " + parser);
...

>I am sorry i must misunderstand something because the compiler doesn't
>like it telling me it cant find the symbols.

It would be mighty useful to you if you figure how
to read those exceptions, and always copy/paste
any you do not understand.

But ultimately, I agree with Lew's suggestion of
preparing an SSCCE.  You will get more help
from an SSCCE short enough to post to the
group, than either posting code snippets, or
linking to a 'problemsource.zip'.

* Said in the tone of a spokesperson/spin doctor,
rather than anyone's mum.

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Daniel Pitts - 24 Sep 2007 15:02 GMT
> > > > > > I get a java.lang.NullPointerException when i do this ?
>
[quoted text clipped - 39 lines]
> I am sorry i must misunderstand something because the compiler doesn't
> like it telling me it cant find the symbols.

I'm sorry, I had a typo.  I added a spurious "()"
Try this: DocumentBuilderFactory.newInstance().newDocumentBuilder()

Also, try using you own discretion when copying peoples examples.  You
aren't going to be successful as a programmer unless you can figure
things out on your own.
Andrew Thompson - 24 Sep 2007 15:43 GMT
..
>..try using you own discretion when copying peoples examples.  You
>aren't going to be successful as a programmer unless you can figure
>things out on your own.

Wadd'ya' mean 'batteries not included'?!  
(bawls) I want my money bac...(..oh)

Signature

Andrew Thompson
http://www.athompson.info/andrew/

gert - 25 Sep 2007 00:50 GMT
It works thanks all :)

As suggested to name my class Query instead of query I get this?

"javax.servlet.ServletException: PWC1397: Wrapper cannot find servlet
class Query or a class it depends on"

When I rename it back to query it works fine?
What config file do I need to change?
I changed this in web.xml

"<servlet-class>Query</servlet-class>"

and this in my servlet

"public class Query extends HttpServlet"

What els am I forgetting?
Andrew Thompson - 25 Sep 2007 01:05 GMT
> As suggested to name my class Query instead of query I get this?
>
> "javax.servlet.ServletException: PWC1397: Wrapper cannot find servlet
> class Query or a class it depends on"
...
> What els am I forgetting?

Try refreshing/restarting the server.

Andrew T.
gert - 25 Sep 2007 01:47 GMT
> > As suggested to name my class Query instead of query I get this?
>
[quoted text clipped - 6 lines]
>
> Andrew T.

Yep that seems to do it thanks :)

PS Can you make a webservice (wsdl) run as a servlet example.com/
some_soap_service ?
Andrew Thompson - 25 Sep 2007 02:45 GMT
...
> PS Can you make a webservice (wsdl) run as a servlet example.com/
> some_soap_service ?

Not sure.  You might be better asking that
question on a new thread with more specific*
subject line.

* More specific to your latest question.
Lew - 25 Sep 2007 03:36 GMT
> ....
>> PS [sic] Can you make a webservice [sic] (wsdl [sic]) run as a servlet example.com/
[quoted text clipped - 5 lines]
>
> * More specific to your latest question.

A web service is not the same thing as a WSDL specification.  The common way
to run a web service in the Java universe is via a servlet, yes.

Signature

Lew

Lew - 24 Sep 2007 00:38 GMT
>> How can i [sic] make a BufferedReader a inputstream [sic], can i just cast it to a
>> input stream [sic] ?

No.

>> Parse requires a inputstream or string [sic]?

Or an InputSource, according to the Javadocs.

>> import javax.xml.parsers.DocumentBuilder;
>> import org.w3c.dom.Document;
[quoted text clipped - 8 lines]
>
> InputStream in  = (InputStream) request.getReader();

That's because there is no 'is-a' relationship between a Stream and a Reader.
 Casting is used to make a supertype appear as a subtype, and only works when
the runtime instance actually is of that subtype.  This time, getReader()
returns an instance that is not in any way an InputStream, so the cast cannot
work.  It shouldn't even compile.

> Any idea's [sic]?

<http://java.sun.com/javase/6/docs/api/javax/xml/parsers/DocumentBuilder.html#par
se(org.xml.sax.InputSource
)>
chain to
<http://java.sun.com/javase/6/docs/api/org/xml/sax/InputSource.html>
specifically
<http://java.sun.com/javase/6/docs/api/org/xml/sax/InputSource.html#InputSource(j
ava.io.Reader
)>

Side note: spelling counts.  In particular, case and whitespace count.

When posting code, copy-and-paste source (without any TAB characters) directly
into the message.  That will prevent transcription errors, such as "Documnet
doc;" above.

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.