xml byte array

S

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
 
T

Thomas Fritsch

Sashi said:
[...] 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);
 
S

Sashi

Thomas said:
Sashi said:
[...] 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);
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
 
O

Oliver Wong

Sashi said:
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
 
S

Sashi

Oliver said:
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
Thanks, Oliver, I'll check it out.
Sashi
 
I

iksrazal

Sashi escreveu:
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
 
S

Sashi

Sashi escreveu:


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
{
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
In terms of effort, extracting the byte array is easier on the
programmer.
Sashi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,279
Latest member
LaRoseDermaBottle

Latest Threads

Top