Java之XML操作:从XML中直接获取数据
本文介绍如何将数据记录在XML文件中,然后通过DOM4J直接从XML中读取到数据。
依赖包:
<dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency>
工具类代码:
1 package testTraffic.utils; 2 3 import org.dom4j.Document; 4 import org.dom4j.DocumentException; 5 import org.dom4j.Element; 6 import org.dom4j.io.SAXReader; 7 import java.io.File; 8 import java.io.IOException; 9 import java.io.InputStream; 10 import java.net.URISyntaxException; 11 import java.util.ArrayList; 12 import java.util.LinkedHashMap; 13 import java.util.List; 14 import java.util.Map; 15 16 /** 17 * XmlUtils class 18 * 19 * @author gongxingrui 20 * @date 2018-12-07 21 **/ 22 public class XmlUtils { 23 24 25 public static Document readXml(String filePath) throws DocumentException { 26 return readXml(new File(filePath)); 27 } 28 29 30 public static Document readXml(File file) throws DocumentException { 31 SAXReader reader = new SAXReader(); 32 return reader.read(file); 33 } 34 35 public static Document readXml(InputStream inputStream) throws DocumentException { 36 SAXReader reader = new SAXReader(); 37 return reader.read(inputStream); 38 } 39 40 41 public static Document readXmlFromResources(String resource) throws DocumentException, IOException, URISyntaxException { 42 SAXReader reader = new SAXReader(); 43 return reader.read(ResourcesUtils.getResourceAsFile(resource)); 44 } 45 46 47 public static String getAttribute(Element element, String attrName) { 48 return element.attributeValue(attrName); 49 } 50 51 public static String getText(Element element) { 52 return element.getText(); 53 } 54 55 public static List<Element> getElements(Element element, String tagName) { 56 return element.elements(tagName); 57 } 58 59 /** 60 * 根据节点名称获取子节点数据 61 */ 62 public static String getElementText(String resource, String qName) throws Exception { 63 Element root = readXmlFromResources(resource).getRootElement(); 64 String text = root.elementText(qName); 65 return text.trim(); 66 } 67 68 /** 69 * 根据节点名称获取子节点的子节点数据 70 */ 71 public static String getElementText(String resource, String qName, String SubQName) throws Exception { 72 Element root = readXmlFromResources(resource).getRootElement(); 73 String text = root.element(qName).elementText(SubQName); 74 return text.trim(); 75 } 76 77 /** 78 * 根据节点名称获取子节点的子节点的数据列表 79 */ 80 public static List<String> getElementTextList(String resource, String qName) throws Exception { 81 List<Element> elementList = readXmlFromResources(resource).getRootElement().element(qName).elements(); 82 List<String> list = new ArrayList<>(); 83 for (Element e : elementList) { 84 list.add(e.getTextTrim()); 85 } 86 return list; 87 } 88 89 90 }
测试代码:
@Test public void testXML6() throws Exception { String path2 = "shelby/data/sql/demo/sql2.xml"; String s = XmlUtils.getElementText(path2, "insert_1"); logger.info(s); List<String> list = XmlUtils.getElementTextList(path2, "del_group"); logger.info(list); String s2 = XmlUtils.getElementText(path2, "del_group_2", "del_2"); logger.info(s2); }
测试结果:
2019-01-28 16:13:14.746 INFO testTraffic.testDemo.testXmlDemo testXML6 : insert into `t_admin_user` (`id`, `user_name`, `user_password`, `del_flag`, `create_time`, `update_time`) VALUES( null,'testuser','testuser123','0','2019-01-21 19:43:58','2019-01-22 19:44:03'); 2019-01-28 16:13:14.763 INFO testTraffic.testDemo.testXmlDemo testXML6 : [delete from t_admin_user WHERE user_name = 'testuser';, delete from t_admin_user WHERE user_name = 'testuser2';] 2019-01-28 16:13:14.768 INFO testTraffic.testDemo.testXmlDemo testXML6 : delete from t_admin_user WHERE user_name = 'testuser2'; Process finished with exit code 0
XML文档:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <mybatisGroup id="预置数据语句"> 3 <del_1 id="del_1" type="delete"> 4 delete from t_admin_user WHERE user_name = 'testuser'; 5 </del_1> 6 <del_2 id="del_2" type="delete"> 7 delete from t_admin_user WHERE user_name = 'testuser2'; 8 </del_2> 9 <insert_1 id="insert_1" type="insert"> 10 insert into `t_admin_user` (`id`, `user_name`, `user_password`, `del_flag`, `create_time`, `update_time`) 11 VALUES( null,'testuser','testuser123','0','2019-01-21 19:43:58','2019-01-22 19:44:03'); 12 </insert_1> 13 14 <del_group> 15 <statement> 16 delete from t_admin_user WHERE user_name = 'testuser'; 17 </statement> 18 <statement> 19 delete from t_admin_user WHERE user_name = 'testuser2'; 20 </statement> 21 </del_group> 22 23 <del_group_2> 24 <del_1> 25 delete from t_admin_user WHERE user_name = 'testuser'; 26 </del_1> 27 <del_2> 28 delete from t_admin_user WHERE user_name = 'testuser2'; 29 </del_2> 30 <del_3> 31 delete from t_admin_user WHERE user_name = 'testuser3'; 32 </del_3> 33 </del_group_2> 34 35 </mybatisGroup>