使用JAXBContext将XML注释添加到封送文件中 [英] Add XML comments into marshaled file using JAXBContext

查看:113
本文介绍了使用JAXBContext将XML注释添加到封送文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将对象编组到XML文件中。如何在该XML文件中添加注释

  ObjectFactory factory = new ObjectFactory(); 
JAXBContext jc = JAXBContext.newInstance(Application.class);
Marshaller jaxbMarshaller = jc.createMarshaller();
jaxbMarshaller.marshal(application,localNewFile);
jaxbMarshaller.marshal(application,System.out);

有些人认为

 <! - 作者日期 - > 

谢谢

解决方案

您可以利用JAXB和StAX并执行以下操作:



演示



如果您希望文档开头的注释可以在使用JAXB编组对象之前将它们写出到目标。您需要确保将 Marshaller.JAXB_FRAGMENT 属性设置为true,以防止JAXB编写XML声明。



< pre class =lang-java prettyprint-override> import javax.xml.bind。*;
import javax.xml.stream。*;

公共类演示{

public static void main(String [] args)抛出异常{
XMLOutputFactory xof = XMLOutputFactory.newFactory();
XMLStreamWriter xsw = xof.createXMLStreamWriter(System.out);

xsw.writeStartDocument();
xsw.writeComment(作者日期);

JAXBContext jc = JAXBContext.newInstance(Foo.class);

Foo foo = new Foo();
foo.setBar(Hello World);

Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT,true);
marshaller.marshal(foo,xsw);

xsw.close();
}

}

域模型(Foo) )

  import javax.xml.bind.annotation.XmlRootElement; 

@XmlRootElement
public class Foo {

private String bar;

public String getBar(){
return bar;
}

public void setBar(String bar){
this.bar = bar;
}

}

输出

 <?xml version =1.0?><! - 作者date  - >< foo>< bar> Hello World< / bar>< / foo> 






UPDATE



使用StAX方法,输出将不会被格式化。如果你想要格式化,下面的代码可能会更适合你:

  import java.io.OutputStreamWriter; 
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

公共类演示{

public static void main(String [] args)抛出异常{
OutputStreamWriter writer = new OutputStreamWriter(System.out,UTF- 8\" );
writer.write(<?xml version = \1.0 \?> \ n);
writer.write(<! - 作者日期 - > \ n);

JAXBContext jc = JAXBContext.newInstance(Foo.class);

Foo foo = new Foo();
foo.setBar(Hello World);

Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
marshaller.setProperty(Marshaller.JAXB_FRAGMENT,true);
marshaller.marshal(foo,作家);

writer.close();
}

}


I'm marshaling objects into an XML file. How can I add comments into that XML file

ObjectFactory factory = new ObjectFactory();
JAXBContext jc = JAXBContext.newInstance(Application.class);
Marshaller jaxbMarshaller = jc.createMarshaller();
jaxbMarshaller.marshal(application, localNewFile);          
jaxbMarshaller.marshal(application, System.out);

to have some think like

<!-- Author  date  -->

Thanks

解决方案

You could leverage JAXB and StAX and do the following:

Demo

If you want the comments at the beginning of the document you can write them out to the target before using JAXB to marshal the objects. You will need to be sure to se the Marshaller.JAXB_FRAGMENT property to true to prevent JAXB from writing the XML declaration.

import javax.xml.bind.*;
import javax.xml.stream.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        XMLOutputFactory xof = XMLOutputFactory.newFactory();
        XMLStreamWriter xsw = xof.createXMLStreamWriter(System.out);

        xsw.writeStartDocument();
        xsw.writeComment("Author  date");

        JAXBContext jc = JAXBContext.newInstance(Foo.class);

        Foo foo = new Foo();
        foo.setBar("Hello World");

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        marshaller.marshal(foo, xsw);

        xsw.close();
    }

}

Domain Model (Foo)

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Foo {

    private String bar;

    public String getBar() {
        return bar;
    }

    public void setBar(String bar) {
        this.bar = bar;
    }

}

Output

<?xml version="1.0" ?><!--Author  date--><foo><bar>Hello World</bar></foo>


UPDATE

With the StAX approach the output won't be formatted. If you want formatting the following may work better for you:

import java.io.OutputStreamWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        OutputStreamWriter writer = new OutputStreamWriter(System.out, "UTF-8");
        writer.write("<?xml version=\"1.0\" ?>\n");
        writer.write("<!--Author  date-->\n");

        JAXBContext jc = JAXBContext.newInstance(Foo.class);

        Foo foo = new Foo();
        foo.setBar("Hello World");

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        marshaller.marshal(foo, writer);

        writer.close();
    }

}

这篇关于使用JAXBContext将XML注释添加到封送文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆