gpt4 book ai didi

c# - jaxb marshal 生成空文件

转载 作者:行者123 更新时间:2023-11-30 04:05:29 27 4
gpt4 key购买 nike

尝试编码(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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com