当前位置:网站首页>XML parsing bean tool class
XML parsing bean tool class
2022-06-26 03:45:00 【thoughtCodes】
xml analysis bean Tool class
Survive by day and develop by night.
talk is cheap, show me the code,make a better result.
Catalog
summary
stay webservice The common expressions in are bean Tool class of
demand :
1.xmltobean
Design thinking
Analysis of implementation ideas
1.xml2bean
// Specify all class Average resolution annotations
xstream.autodetectAnnotations(true);
// Specify class analysis annotations
xstream.processAnnotations(People.class);
//
Use xstream Self contained NoNameCoder structure xstream, This method will cause all special characters not to be escaped
XStream xstream = new XStream(new XppDriver(new NoNameCoder()));
// Use Domdriver And NonameCoder structure xstream, This method can specify xml Encoding mode
XStream xstream = new XStream(new DomDriver(“UTF-8”, new NoNameCoder()));
new Xstream().toXML(bean);
xstreams Utils Encapsulation :
package com.hibase.common.utils;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
/** * xstream Tool class * * @author hufeng * @date 2019/07/09 */
public class XStreamUtils {
private XStreamUtils() {
}
/** * Implement the singleton pattern through static inner classes */
private static class LazyHolder {
private static final XStreamUtils INSTANCE = new XStreamUtils();
}
private static class SingletonXstream {
private static XStream xStream = null;
static {
if (xStream == null) {
xStream = new XStream(new DomDriver());
// Get rid of class attribute
xStream.aliasSystemAttribute(null, "class");
// Avoid the following warnings :Security framework of XStream not initialized, XStream is probably vulnerable
XStream.setupDefaultSecurity(xStream);
}
}
}
public static XStream getInstance() {
return SingletonXstream.xStream;
}
/** * The object is directly converted to XML String format * * @param t * @param <T> * @return */
public static <T> String toXml(T t) {
XStream xstream = XStreamUtils.getInstance();
xstream.allowTypeHierarchy(t.getClass());
xstream.processAnnotations(t.getClass());
return xstream.toXML(t);
}
/** * XML Convert directly to object * * @param xml * @param clazz * @param <T> * @return */
@SuppressWarnings("unchecked")
public static <T> T toBean(String xml, Class<T> clazz) {
XStream xstream = XStreamUtils.getInstance();
xstream.allowTypeHierarchy(clazz);
xstream.processAnnotations(clazz);
T obj = (T) xstream.fromXML(xml);
return obj;
}
}
Expand to achieve
Reference here :github: Simply implement the above process :
Entry level implementation :
: Partial source code implementation .
: The source code to achieve
Performance parameter test :
nothing , Temporarily no
References and recommended readings
- xstream Special characters are escaped .
- Use source code to solve xstream Special character problems .
- Use xmlbeans Tool class .
Welcome to , Dear brother , If it helps you , A little praise and a little attention !~
边栏推荐
- progress bar
- kitti2bag 安装出现的各种错误
- Double carbon bonus + great year of infrastructure construction 𞓜 deep ploughing into the field of green intelligent equipment for water conservancy and hydropower
- After Ali failed to start his job in the interview, he was roast by the interviewer in the circle of friends (plug)
- 2020 summary: industrial software development under Internet thinking
- Uni app custom selection date 2 (September 16, 2021)
- Is it safe for individuals to buy stocks with flush software? How to buy stocks
- Uni app QR code scanning and identification function
- MySQL的视图
- MySQL高级部分( 四: 锁机制、SQL优化 )
猜你喜欢
Evaluation - analytic hierarchy process
ABP framework Practice Series (I) - Introduction to persistence layer
Camera-memory内存泄漏分析(二)
小米电视的网页和珠宝的网页
[hash table] improved, zipper hash structure - directly use two indexes to search, instead of hashing and% every time
机器学习笔记 - 时间序列的趋势分量
You cannot call Glide. get() in registerComponents(), use the provided Glide instance instead
Camera-CreateCaptureSession
TiFlash 函数下推必知必会丨十分钟成为 TiFlash Contributor
. Net core learning journey
随机推荐
kitti2bag 安装出现的各种错误
C # knowledge structure
js实现文字跑马灯效果
“再谈”协议
General operations of asynctask
Mybatis的引入问题invalid
JS array array JSON de duplication
Camera-CreateCaptureSession
个人用同花顺软件买股票安全吗?怎么炒股买股票呢
WebRTC系列-网络传输之7-ICE补充之偏好(preference)与优先级(priority)
Restful API interface design standards and specifications
Kotlin uses viewpager2+fragment+bottomnavigationview to implement the style of the switching module of the bottom menu bar.
"Renegotiation" agreement
Nepal graph learning Chapter 3_ Multithreading completes 6000w+ relational data migration
解决uniapp插件robin-editor设置字体颜色和背景颜色报错的问题
How to prepare for a moving wedding
Dynamic segment tree leetcode seven hundred and fifteen
Mysql database foundation
redux-thunk 简单案例,优缺点和思考
[appium stepping pit] io appium. uiautomator2. common. exceptions. InvalidArgumentException: ‘capabilities‘ are mand