从jaxb生成的xml中删除独立的=“yes”

时间:2023-01-14 18:43:44
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

在这里,我想删除独立的="yes",并希望输出为

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

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

我检查了链接JAXB -从生成的XML中删除' independent ="yes",但是这里的答案是删除complete

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

part

部分

I don't want that...

我不想要……

Please help me..

请帮我. .

1 个解决方案

#1


6  

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

在你的问题中有几个问题需要解决:

ISSUE #1 - Encoding

问题# 1 -编码

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.

“jaxb。“设置时的编码”属性直接影响输出为输出流时的编码。如果您使用的输出(例如Writer)是可以处理它自己的编码的,那么您需要确保您将其作为写入器的一部分来处理。

For More Information

的更多信息

ISSUE #2 - standalone="yes"

问题# 2 -独立= "是的"

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

您可以创建StAX (JSR-173) XMLStreamWriter,将您的StringWriter打包为您的XML输出并对其进行封送。

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.

注意:我是eclipse JAXB (MOXy)的负责人,也是JAXB (JSR-222)专家组的成员。

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.

还有其他JAXB (JSR-222)提供程序,比如MOXy,它们不能作为XML输出的一部分输出独立的=“yes”。

#1


6  

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

在你的问题中有几个问题需要解决:

ISSUE #1 - Encoding

问题# 1 -编码

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.

“jaxb。“设置时的编码”属性直接影响输出为输出流时的编码。如果您使用的输出(例如Writer)是可以处理它自己的编码的,那么您需要确保您将其作为写入器的一部分来处理。

For More Information

的更多信息

ISSUE #2 - standalone="yes"

问题# 2 -独立= "是的"

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

您可以创建StAX (JSR-173) XMLStreamWriter,将您的StringWriter打包为您的XML输出并对其进行封送。

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.

注意:我是eclipse JAXB (MOXy)的负责人,也是JAXB (JSR-222)专家组的成员。

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.

还有其他JAXB (JSR-222)提供程序,比如MOXy,它们不能作为XML输出的一部分输出独立的=“yes”。