users@jaxb.java.net

Re: marshalling

From: Michael Glass <michaelsw_at_ua.fm>
Date: Fri, 21 Aug 2009 11:59:13 -0700 (PDT)

Wolfgang Laun-2 wrote:
>
> The second line was meant to tell you that *you* have to *code* some
> print statement that writes the "<?xml...>" to the destination where
> you marshal to.
>
> I can't tell you the exact statement, but if you marshal to
> System.out, it would be
>
> System.out.println( "<?xml version="1.0" encoding="UTF-8"?>" );

I did it the following way:
            private static String XML_HEADER = "<?xml version=\"1.0\"
encoding=\"UTF-8\"?>";
           
.............................................................................................................
            Marshaller m = ctx.createMarshaller();
           
..............................................................................................................
            m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
           
.............................................................................................................
            byte[] xmlHeaderBytes = getXMLHeaderByteArray(XML_HEADER);
            getOutputStream().write(xmlHeaderBytes, 0,
xmlHeaderBytes.length);
            m.marshal(createObject(obj), getOutputStream());

            private byte[] getXMLHeaderByteArray(String xmlHeader) {
                      char[] chars = xmlHeader.toCharArray();
                      byte[] bytes = new byte[chars.length];
                      for (int i = 0; i < chars.length; i++) {
                          bytes[i] = (byte)chars[i];
                      }
                      return bytes;
           }
-- 
View this message in context: http://www.nabble.com/marshalling-tp24197380p25085560.html
Sent from the java.net - jaxb users mailing list archive at Nabble.com.