user3032973
user3032973

Reputation: 415

jaxb marshal generating empty file

Attempting to marshal a kml file which is running on IIS/java 1.6. The jaxb marshaller is not throwning an error. The file is created but nothing is written to it. Is there problems running jaxb on 1.6?

final Kml __balloonKML = new Kml();
        final de.micromata.opengis.kml.v_2_2_0.Document baloonDocument =__balloonKML.createAndSetDocument();

 OutputStream o = new FileOutputStream("test.kml");
                 try
                    {
                // __balloonKML.marshal(o);
                    Marshaller m = createMarshaller();
                    m.marshal(__balloonKML, o);
                            o.flush(); o.close();
                    }
                    catch (JAXBException _x)
                    {
                      _x.printStackTrace();
                    }

private JAXBContext getJaxbContext()
            throws JAXBException
          {
        JAXBContext jc = null;

              jc = JAXBContext.newInstance(new Class[] { Kml.class });

            return jc;
          }

     private Marshaller createMarshaller()
                throws JAXBException
              {
                Marshaller m = null;
                  m = getJaxbContext().createMarshaller();
                  m.setProperty("jaxb.formatted.output", Boolean.valueOf(true));
                  m.setProperty("com.sun.xml.bind.namespacePrefixMapper", true);
                return m;
              }

Other approach using file that didnt work

 File file = new File(kmlLoc+"//kml//baloonLayer"+msgId+".kml");
                 JAXBContext jaxbContext = JAXBContext.newInstance(Kml.class);
                 Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
                 jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                 jaxbMarshaller.marshal(__balloonKML, file);

Upvotes: 1

Views: 1411

Answers (1)

bdoughan
bdoughan

Reputation: 149017

Make sure you flush()/close (flush()/close()) the OutputStream after marshalling to it.

Demo

When I run a slightly modified version of your code (see below), I get a file with contents produced.

import java.io.*;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        Demo demo = new Demo();
        demo.marshal();
    }

    private void marshal() throws Exception {
        final Kml __balloonKML = new Kml();
        //final de.micromata.opengis.kml.v_2_2_0.Document baloonDocument = __balloonKML.createAndSetDocument();

        OutputStream o = new FileOutputStream("test.kml");
        try {
            // __balloonKML.marshal(o);
            Marshaller m = createMarshaller();
            m.marshal(__balloonKML, o);
            //o.flush();
            o.close();
        } catch (JAXBException _x) {
            _x.printStackTrace();
        }
    }

    private JAXBContext getJaxbContext() throws JAXBException {
        JAXBContext jc = null;

        jc = JAXBContext.newInstance(new Class[] { Kml.class });

        return jc;
    }

    private Marshaller createMarshaller() throws JAXBException {
        Marshaller m = null;
        m = getJaxbContext().createMarshaller();
        //m.setProperty("jaxb.formatted.output", Boolean.valueOf(true));
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        //m.setProperty("com.sun.xml.bind.namespacePrefixMapper", true);
        return m;
    }
}

Output File (test.kml)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<kml/>

Java Model (Kml)

Below is the simplified model class I am using.

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Kml {

}

Upvotes: 2

Related Questions