Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Create a Byte Arrary from a XML File?

 
Natalie Kopple
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 325
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 325
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much for your advice.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic