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
> [...] 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
> 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