• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to Create a Byte Arrary from a XML File?

 
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much for your advice.
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic