my dog learned polymorphism*
The moose likes XML and Related Technologies and the fly likes XML file as String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XML file as String" Watch "XML file as String" New topic
Author

XML file as String

L Goundalkar
Ranch Hand

Joined: Jul 05, 2001
Posts: 395
Hi all,
I am using Thick client and RMI in my application. The client request for person details which is sent as a java.lang.String type.
I want my JAXP parser to parse this XML file directly as String.
Here are the list of Parse() methods in DocumentBuilder class :
parse(java.io.File f)
parse(InputSource is)
parse(java.io.InputStream is)
parse(java.io.InputStream is, java.lang.String systemId)
parse(java.lang.String uri)
In the above methods we can use the one which takes java.io.InputStream, by passing java.io.StringBufferInputStream which is the child.
But in JDK1.3 java.io.StringBufferInputStream is deprecated and they have asked to use java.io.StringReader.
I have searched is there anyway to convert StringReader to InputStream, but i didn't get it.
And since there is no parse() method available in JAXP for taking java.io.Reader as the parameter.
So please tell me how to do it, without using the deprecated class.
Thanks a lot.

------------------
L Goundalkar
lggoundalkar@hotmail.com
Sun Certified Programmer for Java 2 Platform


SCJP, SCWCD
David Kane
Ranch Hand

Joined: Sep 20, 2001
Posts: 65
Can't you use InputSource(Reader´┐ŻcharacterStream) ?
See: http://dom4j.org/javadoc/org/xml/sax/InputSource.html
David
------------------
David Kane
david_kane@houseofyin.com
Author of Software Architecture: Organizational Principles and Patterns
http://www.vraps.com
http://www.houseofyin.com


David Kane<BR> <A HREF="mailto:david_kane@houseofyin.com" rel="nofollow">david_kane@houseofyin.com</A> <BR>Author of <A HREF="http://www.amazon.com/exec/obidos/ASIN/0130290327/ref=ase_electricporkchop/103-0514572-3811868" TARGET=_blank rel="nofollow">Software Architecture: Organizational Principles and Patterns</A><BR><A HREF="http://www.vraps.com" TARGET=_blank rel="nofollow">http://www.vraps.com</A><BR><A HREF="http://www.houseofyin.com" TARGET=_blank rel="nofollow">http://www.houseofyin.com</A>
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
I think one can easily convrt a string to input stream using
InputStream is = new ByteArrayInputStream( aString.toBytes() );
Any comments? Is this a good/efficient way?
Bruce


BJ - SCJP and SCWCD
We love Java programming. It is contagious, very cool, and lot of fun. - Peter Coad, Java Design

Crazy Bikes created by m-Power
David Peterson
author
Ranch Hand

Joined: Oct 14, 2001
Posts: 154
Efficient - yes. Good - possibly not. The parse(InputSource is) method, as suggested by David Kane, is preferable as it handles character-encoding conversions automagically.
StringReader reader = new StringReader(aString);
InputSource inputSource = new InputSource();
inputSource.setCharacterStream(reader);
docBuilder.parse(inputSource);
David
----
peterson_d_s@hotmail.com
L Goundalkar
Ranch Hand

Joined: Jul 05, 2001
Posts: 395
Hi all,
Thanks a lot.
------------------
L Goundalkar
lggoundalkar@hotmail.com
Sun Certified Programmer for Java 2 Platform
jalal ud-din
Greenhorn

Joined: Nov 04, 2004
Posts: 4
Thank you, Bruce Jin, for helping me on that one.

If any of you have tried sending XMLEncoded beans as http request contents, you will have noticed that when you try to decode, javax.servlet.HttpRequest.getInputStream() includes the CRLF line break at the beginning of the content.

Which is very annoying.
Thanks again Bruce.


.increase the peace
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XML file as String
 
Similar Threads
Creating JDOM Document from a String
problem parsing xml string using dom parser
problem parsing xml string using dom parser
location of xsd file
Parsing a String using DOM API