aspose file tools*
The moose likes Java in General and the fly likes How to Create a Byte Arrary from a XML File? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to Create a Byte Arrary from a XML File?" Watch "How to Create a Byte Arrary from a XML File?" New topic
Author

How to Create a Byte Arrary from a XML File?

Natalie Kopple
Ranch Hand

Joined: May 06, 2003
Posts: 325
I have a XML file, abc.xml. How do I create a byte array from that file? Thanks for help.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
1. using the File class, get the size of the file
2. create a new byte[] of that size
3. open the file as a FileInputStream
4. read( b ) directly into the byte[]
5. close the FileInputStream

done!

Bill
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18989
    
    8

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.
Natalie Kopple
Ranch Hand

Joined: May 06, 2003
Posts: 325
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?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
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[].

Bill
Natalie Kopple
Ranch Hand

Joined: May 06, 2003
Posts: 325
Thanks very much for your advice.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18989
    
    8

Natalie Kopple wrote:Do you see any pitfalls with the byte array?


I already pointed out one. But I missed the part where you explained the advantage of the byte array; why would you want to do that?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to Create a Byte Arrary from a XML File?