Add XML comments into marshaled file using JAXBContext
Asked Answered
T

3

1

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

Tompion answered 5/6, 2013 at 16:44 Comment(0)
Y
2

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

}
Yacano answered 6/6, 2013 at 10:0 Comment(0)
T
1

after a lot of research, I've just added:

System.out.append("your comments");           
System.out.flush();
System.out.append("\n");           
System.out.flush();
jaxbMarshaller.marshal(application, localNewFile);
jaxbMarshaller.marshal(application, System.out);
Tompion answered 6/6, 2013 at 8:29 Comment(1)
This won't work. To read the xml file back in, it has to start with the <?xml> header, adding in comments before that will make the file invalid.Vacuole
S
0

If you need xml-comment just in header of Xml there is much simpler solution (one line of code) - custom property of marshaller.

See details here https://mcmap.net/q/641326/-add-xml-comments-into-marshaled-file

I've tested it in MOXy.

Sternway answered 1/6, 2023 at 13:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.