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 / February 2006

Tip: Looking for answers? Try searching our database.

xml byte array

Thread view: 
Sashi - 07 Feb 2006 16:58 GMT
Hi, I'm using TIBCO rv to receive a message. This message has an XML
document. I can read this XML message into a byte array.
Now how do I output this array into a human readable form? (via the
console, for example). Any pointers would be appreciated.
TIA,
Sashi
Thomas Fritsch - 07 Feb 2006 17:37 GMT
> [...] I can read this XML message into a byte array.
> Now how do I output this array into a human readable form? (via the
> console, for example). [...]

byte[] yourByteArray = ...
System.out.write(yourByteArray);

Signature

"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')

Sashi - 07 Feb 2006 18:05 GMT
> > [...] I can read this XML message into a byte array.
> > Now how do I output this array into a human readable form? (via the
[quoted text clipped - 5 lines]
> --
> "Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
That simply seems to output the bit pattern for this bye array.
I did it by reading each byte off the array, casting it to a char and
appending it to a StringBuffer.
Sashi
Oliver Wong - 07 Feb 2006 18:17 GMT
> I did it by reading each byte off the array, casting it to a char and
> appending it to a StringBuffer.
> Sashi

   Generally, you shouldn't just cast byte to characters, as the conversion
from one to the other may be complicated depending on the encoding (UTF-16
uses at least 2 bytes per character, for example). Check out the
InputStreamReader class:

http://java.sun.com/j2se/1.5.0/docs/api/java/io/InputStreamReader.html

<quote>
An InputStreamReader is a bridge from byte streams to character streams: It
reads bytes and decodes them into characters using a specified charset. The
charset that it uses may be specified by name or may be given explicitly, or
the platform's default charset may be accepted.
</quote>

   - Oliver
Sashi - 07 Feb 2006 19:04 GMT
> > I did it by reading each byte off the array, casting it to a char and
> > appending it to a StringBuffer.
[quoted text clipped - 15 lines]
>
>     - Oliver
Thanks, Oliver, I'll check it out.
Sashi
iksrazal@gmail.com - 07 Feb 2006 19:49 GMT
> Hi, I'm using TIBCO rv to receive a message. This message has an XML
> document. I can read this XML message into a byte array.
> Now how do I output this array into a human readable form? (via the
> console, for example). Any pointers would be appreciated.
> TIA,
> Sashi

Why are you reading the file into a byte array if all you want is the
document printed to the console ?

 public static final String getDocumentAsString(Document document)
throws XMLHelperException
 {
   try
   {
     // Create source and result objects
     Source source = new DOMSource(document);
     StringWriter out = new StringWriter();
     Result result = new StreamResult(out);
     TransformerFactory tFactory = TransformerFactory.newInstance();
     Transformer transformer = tFactory.newTransformer();
     transformer.transform(source, result);
     return out.toString();
   }
   catch(Exception e)
   {
     throw new XMLHelperException("XML Document to String Error", e);
   }
 }

Then just do:

 System.out.println(yourString);

HTH,
iksrazal
www.braziloutsource.com
Sashi - 07 Feb 2006 21:41 GMT
> > Hi, I'm using TIBCO rv to receive a message. This message has an XML
> > document. I can read this XML message into a byte array.
[quoted text clipped - 5 lines]
> Why are you reading the file into a byte array if all you want is the
> document printed to the console ?

For the simple reason that TibrvMsg class provides a toByteArray()
method.
>   public static final String getDocumentAsString(Document document)
> throws XMLHelperException
[quoted text clipped - 23 lines]
> iksrazal
> www.braziloutsource.com
In terms of effort, extracting the byte array is easier on the
programmer.
Sashi


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



©2008 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.