java 从 jaxb 生成的 xml 中删除 standalone="yes"
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14152523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove standalone="yes" from jaxb generated xml
提问by vg123
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
在这里,我想删除 standalone="yes" 并希望输出为
<?xml version="1.0" encoding="ISO-8859-1"?>
I checked link JAXB - Remove 'standalone="yes"' from generated XMLbut answers here are removing complete
我检查了链接 JAXB - 从生成的 XML 中删除 'standalone="yes"'但这里的答案正在删除完整
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
part
部分
I don't want that...
我不想那样...
Please help me..
请帮我..
采纳答案by bdoughan
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.encoding"
属性时套直接影响到编码时的输出是一个OutputStream
。如果您使用的输出(例如Writer
)负责处理其自己的编码,那么您需要确保将其作为Writer
.
For More Information
想要查询更多的信息
ISSUE #2 - standalone="yes"
问题#2 - standalone="yes"
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.
注意:我是EclipseLink 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,它们不会standalone="yes"
作为您可以使用的 XML 输出的一部分进行输出。