Skip to main content

XmlUtils-XML解析器工具类

作用​

本工具类主要提供Xml格式的字符串与Java对象之间的转换功能,本类中的方法均为静态方法。

API列表​

/**
* 创建XML解析器对象的方法
*
* @return 返回XML解析器
*/
public static XmlMapper createXmlMapper() {
// to do sth.
}
/**
* 将对象解析为XML数据结构的方法
*
* @param object 待解析的对象
* @return 返回XML数据结构(如果解析失败,则返回null)
*/
public static String serialize(Object object) {
// to do sth.
}
/**
* 将XML数据结构解析为指定对象的方法
*
* @param xml xml文本
* @param cls 指定的转换类型
* @param <T> 解析返回的具体类型
* @return 返回解析后的对象(如果解析失败,则返回null)
*/
public static <T> T deserialize(String xml, Class<T> cls) {
// to do sth.
}

用法​

静态工具类方法,直接使用即可。

演示​

// 创建一个XmlMapper解析器对象
XmlUtils.createXmlMapper();

// 序列化对象为XML
User user = userMapper.selectById(1);
Objects.requireNonNull(user);
String xml = XmlUtils.serialize(user);