Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XML file as String

 
L Goundalkar
Ranch Hand
Posts: 395
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
David Kane
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bruce Jin
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
David Peterson
author
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 395
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Thanks a lot.
------------------
L Goundalkar
lggoundalkar@hotmail.com
Sun Certified Programmer for Java 2 Platform
 
jalal ud-din
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic