c# - jaxb marshal 生成空文件

标签 c# java xml jaxb

尝试编码(marshal)在 IIS/java 1.6 上运行的 kml 文件。 jaxb 编码器不会引发错误。文件已创建,但未向其中写入任何内容。在 1.6 上运行 jaxb 是否有问题?

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;
              }

使用文件的其他方法不起作用

 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);

最佳答案

确保您刷新()/关闭( flush()/close() ) OutputStream编码后。

演示

当我运行代码的稍微修改版本(见下文)时,我得到一个包含生成内容的文件。

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;
    }
}

输出文件(test.kml)

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

Java 模型 (Kml)

下面是我正在使用的简化模型类。

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Kml {

}

关于c# - jaxb marshal 生成空文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20849263/

相关文章:

c# - 由于重载方法,使用 Xamarin Android 的绑定(bind)错误

r - 如何使用表格列表

c# - 如何在asp.net中使用fileupload获取正确的路径?

java - 等待方法的返回值

java - 枚举方法覆盖

c# - 无法在 C# 中读取值 XML 属性

java - 使用jsp将递归结构转换为xml

c# - 为什么我的水平工具条一直垂直堆叠?

c# - 自定义控制台应用程序的标题栏区域

c# - mvc 模板中定义的导航栏在哪里?