aspose file tools*
The moose likes I/O and Streams and the fly likes Servlet to read XML tags from Client Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Servlet to read XML tags from Client" Watch "Servlet to read XML tags from Client" New topic
Author

Servlet to read XML tags from Client

Baseet Ahmed
Ranch Hand

Joined: Dec 18, 2006
Posts: 224
Hi,
I want to know that how to read xml tags from a servlet.
e.g The example is just rough text)
The client has a file called abc.xml
Client has sent a request to server (Servlet1.java). containing xml tags
(note that there is no html or jsp file).
Content as follows:
<?xml version="1.0" encoding="UTF-8"?>
<IRequest>
<Header>
</Header>
<Body>
<Abc>abc</Abc>
<AID>123</AID>
<XYZ>pqr</XYZ>
</Body>
</IRequest>

Now the servlet got the request from client.
The content type is text/xml.

Now my quest is how to read those parameters which are mentioned in xml file(above).
Say for reading http request parameters we use: request.getParameter(paramname);
Similarly for reading the parameter,say "AID", what should I use.


Thank you in advance.


***********************************
Tip: Modesty is all virtue.
***********************************



[BPSouther: Removed 'Urgent' from post]
[ August 09, 2007: Message edited by: Ben Souther ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39543
    
  27
There aren't any parameters, so you'll need to work differently. You need to get the input stream (via request.getInputStream), and pass that to an XML parser. Are you familiar with XML parsing?


Ping & DNS - updated with new look and Ping home screen widget
Amol Nayak
Ranch Hand

Joined: Oct 26, 2006
Posts: 218
Since your xml file size is not huge you can safely use DOM parser.

This link might help you understand xml parsing using DOM.

http://java.sun.com/developer/codesamples/xml.html
Baseet Ahmed
Ranch Hand

Joined: Dec 18, 2006
Posts: 224
Thank you Amol for giving URL.
I need to know how to get xml in Servlet from client.

Thank you Ulf,
Well I am not having much more experience on XML parsing.
Right now I need to know XML to be read in Servlet from client.
Just we do with html/jsp and servlet.(Using request.getParameter() and response to send response to users).

I tell you my requirement in detail.Please have a look on this:
XML file[abc.xml] (at client side)
<?xml version="1.0" encoding="UTF-8"?>
<ImageRequest>
<Header>
</Header>
<Body>
<PLocC>Abcxyz</PLocC>
<fId>43</fId>
<No>1</No>
</Body>
</ImageRequest>

Servlet code(Xyz.java) (server side)
Now here I want to access(read etc.) above properties(PLoc,fId,No) in servlet.
Before that I also want to know how to get the xml file before reading parameters.(Using URLConnection or any other way).

Please help me out.

Thank you in advance.

***********************************
Tip: Modesty is all virtue.
***********************************
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39543
    
  27
You get the XML by using the method call I mentioned. You can process it through SAX, DOM or XPath - any of these will work.

You will need to learn how to handle XML via the JAXP API. If you haven't done this before, then this is a good time to learn it. XML processing is happening everywhere these days - it's useful to know how to do it using Java.
Baseet Ahmed
Ranch Hand

Joined: Dec 18, 2006
Posts: 224
Hi Ulf,
Can you please tell me how to retrieve xml file coming from Client in detail?
You said use the method call specified by you(getInputStream()),but what I understand, for reading data of stream we use the same.Will it be used for reading attached xml file?
I have used one method SAX XML Parser package)
Code snippet:
XMLReader xread = XMLReaderFactory.createXMLReader();
pobj("dude");
//SAXParserFactory factory = SAXParserFactory.newInstance();
//SAXParser saxParser = factory.newSAXParser();
//DefaultHandler handler = new parseXML();

ParseXML handler = new ParseXML();

xread.setContentHandler(handler);
//xread.setErrorHandler(handler);

FileReader fr = new FileReader("xmlfile path");//I want read from client.

xread.parse(new InputSource(fr));

//saxParser.parse( fr, handler);
}
....

Thank you in advance.


***********************************
Tip: Modesty is all virtue.
***********************************
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Do you want the client to be able to select a file from their machine and upload it to the server?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Baseet Ahmed
Ranch Hand

Joined: Dec 18, 2006
Posts: 224
No in my case client is not going to select and upload file to server.
Actually,the client is using their application(client-side) and they are sendig a http request(POST) to us along with xml file's content attached(may be in request header) using SOAP action.(I am not having idea on SOAP in detail). Now I want to read xml content and response accordingly.

Please let me know the steps/guidelines to achieve this.


Thank you.
Baseet Ahmed

***********************************
Tip: Modesty is all virtue.
***********************************
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

It depends on how the client is sending the XML.
If it's sending it as an embedded form parameter, it will be as easy as calling:
.

If it's not embedded in a form parameter, you'll need to read the servletInputStream.
See:
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletRequest.html#getInputStream()

Depending on what you're using for a parser, you might be able to feed the ServletInputStream right into it. Otherwise, you'll need wrap it with another structure or write it to file before parsing the XML.
Baseet Ahmed
Ranch Hand

Joined: Dec 18, 2006
Posts: 224
Hi Ben,
Actually the bottleneck is:
what will be the sytax of reading xml content from client.
I got,"how to parse xml" but before that, I have to accept http request with attached Xml data.
If I use request.getInputStream(), it will return InputStream's object.
Now how I can get xml file(or xml data) from this input stream's object, which is coming from client.

For reading from command line we use args[] array object(For standalone application). Similarly what I should use for web-based application)

I am new to this.
Let me know.
Thank you.


***********************************
Tip: Sincerity is mother of all acheivements.
***********************************
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Baseet,
We just happen to have a forum dedicated to IO/Streams.
Before moving there, however, I'll mention the only 'gotcha' specific to servlets.

You can either read the inputStream or call getParameter; not both.
Once you call getInpuStream, you won't be able to use getParameter.
The opposite is also true, if you call getParameter, you won't be able to call getInputStream.

Moving...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39543
    
  27
Originally posted by Baseet Ahmed:
Now how I can get xml file(or xml data) from this input stream's object, which is coming from client.


You don't need to retrieve the XML from the InputStream. You can pass the stream to a DocumentBuilder or a SAXParser, depending on whether you want to use the DOM or SAX APIs to do the parsing. For your purpose, I'd recommend using DOM. The code could look something like this:



Once you have the Document object, you can either traverse it recursively until you find the element you're looking for, or use XPath to access it directly.
[ August 13, 2007: Message edited by: Ulf Dittmer ]
williams ilenikhena
Greenhorn

Joined: Nov 27, 2006
Posts: 7
hi all,

i found this thread very informative.

my situation is rather similar becos i'm trying to do a HTTP post from a J2SE application on one pc to a web server on another pc only that my post contains xml.

this is fine and on the web server my with my servlet i do a request.getInputStream()...like so

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = factory.newDocumentBuilder();
Document doc = parser.parse(new InputSource(request.getInputStream()));


i have tried to parse this Inputstream with DOM, SAX and JOX parsers for each parser at the point where i pass the inputstream to the parser i get same exception below:

WARNING: Servlet.service() for servlet ManageSyncronizeServlet threw exception
java.net.UnknownHostException: ]www.baycloud.com
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177)
at java.net.Socket.connect(Socket.java:507)
at java.net.Socket.connect(Socket.java:457)
at sun.net.NetworkClient.doConnect(NetworkClient.java:157)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:365)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:477)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:214)
at sun.net.www.http.HttpClient.New(HttpClient.java:287)
at sun.net.www.http.HttpClient.New(HttpClient.java:299)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:792)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:744)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:669)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:913)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:973)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startEntity(XMLEntityManager.java:905)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startDTDEntity(XMLEntityManager.java:872)
at com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.setInputSource(XMLDTDScannerImpl.java:282)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDispatcher.dispatch(XMLDocumentScannerImpl.java:1021)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:368)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:834)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:148)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1242)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:375)
at com.wutka.jox.JOXSAXBeanInput.readObject(JOXSAXBeanInput.java:153)
at com.wutka.jox.JOXBeanReader.readObject(JOXBeanReader.java:72)
at retailpay.server.servlet.ManageSyncronizeServlet.doPost(ManageSyncronizeServlet.java:31)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:407)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:825)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:738)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:526)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)

i've been looking for solutions to this.


i would be grateful if anyone could offer any suggestions.

thank you all.
[ September 13, 2007: Message edited by: williams ilenikhena ]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8707
    
    6

I was going to say that you had a bad character in your url:

But it appears that the extra "]" was caused by a bad nesting of bold and URL tags. Is this correct?


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
williams ilenikhena
Greenhorn

Joined: Nov 27, 2006
Posts: 7
yep,you're right, thanks.

Also i figured out the problem.

the www.baycloud... was from the xml generated and sent by my client; in the <?xml ... ?> tag definition, hence the java.net... exception.

took this out and tis all sorted.

thanks
Baseet Ahmed
Ranch Hand

Joined: Dec 18, 2006
Posts: 224
You don't need to retrieve the XML from the InputStream. You can pass the stream to a DocumentBuilder or a SAXParser, depending on whether you want to use the DOM or SAX APIs to do the parsing. For your purpose, I'd recommend using DOM.



Thank you Ulf for reply.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet to read XML tags from Client
 
Similar Threads
Why is the include JSP Standard Action like SSI?
Problem in Reading XML file through Java
how to send parameter from html to jsp??
org.apache.jasper.JasperException: java.lang.ArrayIndexOutOfBoundsException: 8
character encoding problem