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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With