如果有人回答了这个问题,我深表歉意,但我一直在使用的搜索词(即 JAXB @XmlAttribute condensed 或 JAXB XML marshal to字符串不同的结果)没有得出任何结果。
我正在使用 JAXB 取消/编码用 @XmlElement
和 @XmlAttribute
注释注释的对象。我有一个格式化程序类,它提供了两种方法——一种包装编码方法并接受要编码的对象和一个 OutputStream
,另一种只接受对象并将 XML 输出作为字符串返回。不幸的是,这些方法不会为相同的对象提供相同的输出。编码到文件时,内部用 @XmlAttribute
标记的简单对象字段打印为:
<element value="VALUE"></element>
当编码为字符串时,它们是:
<element value="VALUE"/>
对于这两种情况,我都更喜欢第二种格式,但我很好奇如何控制差异,并且无论如何都会满足于它们相同。我什至创建了一个静态编码器,两种方法都使用它来消除不同的实例值。格式化代码如下:
/** Marker interface for classes which are listed in jaxb.index */
public interface Marshalable {}
/** Local exception class */
public class XMLMarshalException extends BaseException {}
/** Class which un/marshals objects to XML */
public class XmlFormatter {
private static Marshaller marshaller = null;
private static Unmarshaller unmarshaller = null;
static {
try {
JAXBContext context = JAXBContext.newInstance("path.to.package");
marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
unmarshaller = context.createUnmarshaller();
} catch (JAXBException e) {
throw new RuntimeException("There was a problem creating a JAXBContext object for formatting the object to XML.");
}
}
public void marshal(Marshalable obj, OutputStream os) throws XMLMarshalException {
try {
marshaller.marshal(obj, os);
} catch (JAXBException jaxbe) {
throw new XMLMarshalException(jaxbe);
}
}
public String marshalToString(Marshalable obj) throws XMLMarshalException {
try {
StringWriter sw = new StringWriter();
return marshaller.marshal(obj, sw);
} catch (JAXBException jaxbe) {
throw new XMLMarshalException(jaxbe);
}
}
}
/** Example data */
@XmlType
@XmlAccessorType(XmlAccessType.FIELD)
public class Data {
@XmlAttribute(name = value)
private String internalString;
}
/** Example POJO */
@XmlType
@XmlRootElement(namespace = "project/schema")
@XmlAccessorType(XmlAccessType.FIELD)
public class Container implements Marshalable {
@XmlElement(required = false, nillable = true)
private int number;
@XmlElement(required = false, nillable = true)
private String word;
@XmlElement(required = false, nillable = true)
private Data data;
}
调用marshal(container, new FileOutputStream("output.xml"))
和marshalToString(container)
的结果如下:
输出到文件
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:container xmlns:ns2="project/schema">
<number>1</number>
<word>stackoverflow</word>
<data value="This is internal"></data>
</ns2:container>
和
输出为字符串
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:container xmlns:ns2="project/schema">
<number>1</number>
<word>stackoverflow</word>
<data value="This is internal"/>
</ns2:container>
最佳答案
看起来这可能是 JAXB 中的一个“错误”。查看源代码,对 marshal() 的调用根据输出/编写器类型参数创建不同的编写器:
public void marshal(Object obj, OutputStream out, NamespaceContext inscopeNamespace) throws JAXBException {
write(obj, createWriter(out), new StAXPostInitAction(inscopeNamespace,serializer));
}
public void marshal(Object obj, XMLStreamWriter writer) throws JAXBException {
write(obj, XMLStreamWriterOutput.create(writer,context), new StAXPostInitAction(writer,serializer));
}
编写者的实现在处理“空元素”方面有所不同。以上代码来自:
jaxb-ri\runtime\src\com\sun\xml\bind\v2\runtime\MarshallerImpl.java.
您正在创建的两个作者是:
jaxb-ri\runtime\src\com\sun\xml\bind\v2\runtime\output\UTF8XmlOutput.java
jaxb-ri\runtime\src\com\sun\xml\bind\v2\runtime\output\XMLStreamWriterOutput.java
关于java - JAXB 将 XML 编码为 OutputStream 与 StringWriter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3023676/