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

G

gert

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.
 
D

Daniel Pitts

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.
 
G

gert

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);
 
G

gert

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);

casting doesn't work

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

Any idea's ?
 
G

gert

casting doesn't work

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

Any idea's ?

never mind found it :)

BufferedReader in = request.getReader();
while((input = in.readLine()) != null)
{
xmlText = xmlText + input;
in.close();
}
 
L

Lew

gert said:
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.
casting doesn't work

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/a...ntBuilder.html#parse(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(java.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.
 
D

Daniel Pitts

never mind found it :)

BufferedReader in = request.getReader();
while((input = in.readLine()) != null)
{
xmlText = xmlText + input;
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));
 
G

gert

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 ?
 
G

gert

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)
 
D

Daniel Pitts

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)

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()
 
G

gert

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)

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()

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));
 
D

Daniel Pitts

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)
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()

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));

Add somewhere:
parser = DocumentBuilderFactory().newInstance().newDocumentBuilder()
 
G

gert

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)
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()
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));

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.
 
L

Lew

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.

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:
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.
 
A

Andrew Thompson

gert said:
..
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.

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

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200709/1
 
D

Daniel Pitts

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)
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()
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));
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.

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.
 
A

Andrew Thompson

Daniel Pitts wrote:
..
..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)
 
G

gert

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?
 
A

Andrew Thompson

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top