Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 532
vg123 Avatar asked Jan 04 '13 06:01

vg123


People also ask

What is standalone YES in XML?

The XML standalone element defines the existence of an externally-defined DTD. In the message tree it is represented by a syntax element with field type XML. standalone. The value of the XML standalone element is the value of the standalone attribute in the XML declaration.

What is JAXB marshal?

The JAXB Marshaller interface is responsible for governing the process of serializing Java content trees i.e. Java objects to XML data. This marshalling to XML can be done to variety of output targets.

What is JAXB in Java?

JAXB simplifies access to an XML document from a Java program by presenting the XML document to the program in a Java format. The first step in this process is to bind the schema for the XML document into a set of Java classes that represents the schema.


1 Answers

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

  • http://blog.bdoughan.com/2011/08/jaxb-and-java-io-files-streams-readers.html

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.

  • http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html
like image 176
bdoughan Avatar answered Oct 12 '22 15:10

bdoughan