Hm, that will usually work, but there's a subtle bug that can bite when you're not expecting it. The read() method isn't guaranteed to actually read the full contents in one read - you may get only a partial read. You can do the read in a loop, ensuring that we read until the end of file is reached. Or more easily, you can use a RandomAccessFile rather than a FileInputstream, and use the readFully() method (which is guaranteed to read the number of bytes in the array, or throw an exception if it can't.
Note that it's a bit unusual to read an XML file into a byte array - if you're going to do any serious parsing of the file, you probably want to take advantage of an XML parser first. But perhaps there's some reason you really need a byte array instead. Your call.
Mike Simmons wrote:Note that it's a bit unusual to read an XML file into a byte array - if you're going to do any serious parsing of the file, you probably want to take advantage of an XML parser first. But perhaps there's some reason you really need a byte array instead. Your call.
Unusual, yes, but parsing XML from a byte array instead of a file can also raise other obscure issues. For example if you have a relative URL in your XML (perhaps in its DTD declaration) then the parser will understand what it's relative to if it's parsing from a file, but not if it's parsing from a byte array.
However we know nothing about what this byte array is actually for. Perhaps it's not even going to be parsed.
Joined: May 06, 2003
Thanks for the feedback. I am going to use that create bye array as a parameter in a "tranform" method to transform the XML to a text file. I have a XSD file and XSL file ready for the transformation. Do you see any pitfalls with the byte array?
Author and all-around good cowpoke
Joined: Mar 22, 2000
A possible problem is that XML documents may have any Unicode character and may have been created with an encoding which is not what you expect.
If you are going to be using javax.xml.transform note that you could be using a variety of StreamSouce inputs which might be more convenient than the byte.