pom.xml
<dependencies>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency>
<!--dom4j-->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--firstjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<!--log4j-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
Dom4jUtil
public class Dom4jUtil {
/**
* 根据XML文件路径获取Document对象
* @param pathUrl XML文件路径
* @return document
*/
public static Document getDocument(String pathUrl){
//创建SAXReader对象
SAXReader reader = new SAXReader();
Document document = null;
try {
//读取XML文件并返回document对象
document = reader.read(pathUrl);
} catch (DocumentException e) {
e.printStackTrace();
}
return document;
}
/**
* 展示文档内容
* @param document 文档对象
*/
public static void showElements(Document document){
//获取文档的根节点
Element rootElement = document.getRootElement();
//获取根节点下的子节点(迭代器的方式)
Iterator<Element> it = rootElement.elementIterator();
//迭代器迭代
while (it.hasNext()){
//获取子节点
Element element = it.next();
//element.getName()获得当前元素的标签名
String name = element.getName();
System.out.println("当前元素的标签名:"+name);
//获取子节点的属性上的值并打印
//String id = element.attribute("id").getValue();
String id = element.attributeValue("id");
System.out.println(id);
//获取根节点下的子节点(集合的方式)
/*
* element.elements("标签名")
* 获得element元素下的所有标签名与方法参数一样的元素
* */
List<Element> childElement = element.elements();
for (Element e : childElement) {
//只有最底层的元素getText()才会有值
//获得当前元素中包含的值
//例如 <a>aaa</a> aaa就是我们获得的值
System.out.println(e.getText());
}
System.out.println("----------------------------");
}
}
/**
* 根据文档对象创建XML文件
* @param document 文档对象
* @throws IOException
*/
public static void createXMLByDocument(Document document) throws IOException {
//创建文件
String filePath = "d:\\studentInfo.xml";
File file = new File(filePath);
if (!file.isFile()){
file.createNewFile();
}
//创建输出流对象
FileOutputStream fos = new FileOutputStream(file);
//获得格式化对象
OutputFormat outputFormat = OutputFormat.createPrettyPrint();
//参数: 输出流,格式化对象
XMLWriter writer = new XMLWriter(fos,outputFormat);
//写出文件
writer.write(document);
/*
* 多个流关闭流程
* 向上依次关闭
* */
writer.close();
fos.close();
}
}
JsonConvert
public class JsonConvert {
/**
* 将json字符串转换为对象
* @param jsonStr json字符串
* @param t
* @param <T> 转化成的对象类型
* @return 返回值类型
*/
public static <T> T jsonToObject(String jsonStr, Class<T> t) {
return JSON.parseObject(jsonStr, t);
}
/**
* 将对象转换为字符串
*
* @param o
* @return
*/
public static String objectToString(Object o) {
return JSON.toJSONString(o);
}
}
Dom4jTest.class
public class Dom4jTest {
@Test
public void getDocument(){
String pathUrl = "src\\main\\resources\\doc\\student.xml";
Document document = Dom4jUtil.getDocument(pathUrl);
System.out.println(document);
Dom4jUtil.showElements(document);
}
@Test
public void createXMLByDocument() throws IOException {
//创建新的文档对象
Document document = DocumentHelper.createDocument();
//添加根节点(元素)
/*
* 注意: 不是最底层元素不要设置 .addText()
* */
Element rootElement = document.addElement("class");
rootElement.addElement("student").addAttribute("id","001")
.addAttribute("classNo","Java1902").addText("James Strachan");
rootElement.addElement("student").addAttribute("id","002")
.addAttribute("classNo","Java1902").addText("James Strachan");
Dom4jUtil.createXMLByDocument(document);
}
}
FastJsonTest.class
public class FastJsonTest {
@Test
public void jsonToObject(){
//拼接json字符串
String jsonStr = "{\"userId\":" + "\"1\"," +
"\"userName\":" + "\"坤坤\"," +
"\"age\":" + "\"22\"}";
Object o = JsonConvert.jsonToObject(jsonStr, User.class);
System.out.println(o);
}
@Test
public void objectToJson(){
User user = new User();
user.setUserId(1);
user.setUsername("阳阳");
user.setAge(22);
String s = JsonConvert.objectToString(user);
System.out.println(s);
}
}