尝试编组在IIS / java 1.6上运行的kml文件。 jaxb marshaller并没有抛出错误。该文件已创建,但未写入任何内容。在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);
答案 0 :(得分:2)
确保在编组后{(1}}冲洗()/关闭(flush()
/ close()
)。
<强>演示强>
当我运行稍微修改过的代码版本(见下文)时,我会得到一个包含内容的文件。
OutputStream
输出文件(test.kml)
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;
}
}
Java模型(Kml)
以下是我正在使用的简化模型类。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<kml/>