java实体类转xml格式(jaxb和xstrem)(⼀)1.javxb⽅式
定义可以接⼝xmlTag ,model时效表⽰可以转化为xml格式
/**
* 描述:XmlTag
*
* @author 归墟
* @date 2021/2/25 20:31
*/
public interface XmlTag {
/**
* 转换为xml格式
*
* @return string 类型失败为null
*/
String toXml();
}
java xml是什么定义jaxbXmltag 标记选⽤的⽅式转化⽅式
/**
* 描述:XmlTag
*
* @author 归墟
* @date 2021/2/24 10:42
*/
public interface JaxbXmlTag extends XmlTag{
/**
* 处理转换
*
* @return f返回
* @throws Exception 异常
*/
default String handToXml() throws Exception {
StringWriter writer = new StringWriter();
XMLStreamWriter streamWriter = wInstance().createXMLStreamWriter(writer); //使⽤动态代理模式, 对streamWriter功能进⾏⼲涉调整
XMLStreamWriter cdataStreamWriter = (XMLStreamWriter) wProxyInstance(
new JaxbCdataHandler(streamWriter)
);
JAXBContext context = Class());
Marshaller mar = ateMarshaller();
mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
mar.setProperty(Marshaller.JAXB_ENCODING, ENCODING);
//⾃定义
NamespacePrefixMapper mapper = getNamespacePrefix();
if (mapper != null) {
mar.setProperty("l.bind.namespacePrefixMapper", mapper);
}
mar.marshal(this, cdataStreamWriter);
String();
}
/**
* ⾃动以根号命名空间前缀
*
* @return 前缀消息
*/
NamespacePrefixMapper getNamespacePrefix();
/**
* 默认utf-8编码
*/
String ENCODING = "UTF-8";
}
⾃定义数据转化adapter ,可以处理cdata格式
public class JaxbStringCdataAdapter extends XmlAdapter<String, String> {
@Override
public String unmarshal(String v) {
return v;
}
@Override
public String marshal(String v) {
return CdataConstant.PRE + v + CdataConstant.SUFFIX;
}
}
处理cdata格式,动态代理转化cdata 避免符号被转意
public class JaxbCdataHandler implements InvocationHandler {
private static Method gWriteCharactersMethod = null;
static {
try {
gWriteCharactersMethod = XMLStreamWriter.class
.getDeclaredMethod("writeCharacters", char[].class, int.class, int.class);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
private final XMLStreamWriter writer;
public JaxbCdataHandler(XMLStreamWriter writer) {
this.writer = writer;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (gWriteCharactersMethod.equals(method)) {
if (!(args[0] instanceof char[])) {
return method.invoke(writer, args);
}
String text = new String((char[]) args[0]);
if (text.startsWith(CdataConstant.PRE) && dsWith(CdataConstant.SUFFIX)) { writer.writeCData(text.substring(9, text.length() - 3));
return null;
}
}
return method.invoke(writer, args);
}
}
实体demo
@Data
@AllArgsConstructor
@NoArgsConstructor
@XmlRootElement(name = "root", namespace = "name-sp")
@Slf4j
public class JaxbXmlDemo implements JaxbXmlTag {
@XmlElement(name = "id")
@Getter(onMethod = @__(@XmlTransient))
private String no;
@XmlJavaTypeAdapter(value = JaxbStringCdataAdapter.class)
@XmlElement(name = "msg")
@Getter(onMethod = @__(@XmlTransient))
private String text;
private String token;
@Override
public String toXml() {
try {
return this.handToXml();
} catch (Exception e) {
e.printStackTrace();
<("失败===数据为{}", JSONString(this));
}
return null;
}
@Override
public NamespacePrefixMapper getNamespacePrefix() {
return new NamespacePrefixMapper() {
@Override
public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) { return "test-prefix";
}
};
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论