vg123
vg123

Reputation: 265

Remove standalone="yes" from jaxb generated xml

public final String getMessage() {
            JAXBContext jaxbContext;
            StringWriter sw = new StringWriter();
            try {
                jaxbContext = JAXBContext.newInstance(Login.class);        
                Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
            
                jaxbMarshaller.setProperty("jaxb.encoding", "ISO-8859-1");                      
                        jaxbMarshaller.marshal(this, sw);
            } catch (JAXBException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return sw.toString();
        }

This is the code what I'm using..and I'm getting output as following.

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>

Here I want to remove standalone="yes" and want output as

<?xml version="1.0" encoding="ISO-8859-1"?>

I checked link Remove 'standalone="yes"' from generated XML but answers here are removing complete

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

part

I don't want that.

Upvotes: 9

Views: 20588

Answers (2)

Shivendra Saxena
Shivendra Saxena

Reputation: 49

I found a simple solution for this problem. Disable the auto generated header by:

marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", false);

This will disable the auto generated header, now we can add our custom headers like this:

marshaller.setProperty("com.sun.xml.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");

As whole this code looks like:

marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", false);
marshaller.setProperty("com.sun.xml.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");

Upvotes: 1

bdoughan
bdoughan

Reputation: 149037

There are a couple of issues that need to be addressed in your question:

ISSUE #1 - Encoding

The "jaxb.encoding" property when sets directly affects the encoding when the output is an OutputStream. If you are using an output that (such as Writer) that is reponsible for handling its own encoding then you need to make sure that you handle that as part of the Writer.

For More Information

ISSUE #2 - standalone="yes"

You can create a StAX (JSR-173) XMLStreamWriter to wrap your StringWriter for your XML output and marshal to that.

import java.io.*;
import javax.xml.bind.*;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.stream.*;

@XmlRootElement
public class Login {

    private JAXBContext jaxbContext;
    private XMLOutputFactory xmlOutputFactory;

    public Login() {
        try {
            jaxbContext = JAXBContext.newInstance(Login.class);
            xmlOutputFactory = XMLOutputFactory.newFactory();
        } catch(Exception e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        Login demo = new Login();
        System.out.println(demo.getMessage());
    }

    public final String getMessage() {
        try {
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
            jaxbMarshaller.setProperty("jaxb.encoding", "ISO-8859-1");
            jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(baos, (String) jaxbMarshaller.getProperty(Marshaller.JAXB_ENCODING));
            xmlStreamWriter.writeStartDocument((String) jaxbMarshaller.getProperty(Marshaller.JAXB_ENCODING), "1.0");
            jaxbMarshaller.marshal(this, xmlStreamWriter);
            xmlStreamWriter.writeEndDocument();
            xmlStreamWriter.close();
            return new String(baos.toByteArray());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

}

Output

<?xml version="1.0" encoding="ISO-8859-1"?><login></login>

ALTERNATE APPROACH

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

There are other JAXB (JSR-222) providers such as MOXy that do not output standalone="yes" as part of the XML Output that you can use.

Upvotes: 8

Related Questions