Java之XML操作:从XML中直接获取数据
<dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency>
package testTraffic.utils; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; /** * XmlUtils class * * @author gongxingrui * @date 2018-12-07 **/ public class XmlUtils { public static Document readXml(String filePath) throws DocumentException { return readXml(new File(filePath)); } public static Document readXml(File file) throws DocumentException { SAXReader reader = new SAXReader(); return reader.read(file); } public static Document readXml(InputStream inputStream) throws DocumentException { SAXReader reader = new SAXReader(); return reader.read(inputStream); } public static Document readXmlFromResources(String resource) throws DocumentException, IOException, URISyntaxException { SAXReader reader = new SAXReader(); return reader.read(ResourcesUtils.getResourceAsFile(resource)); } public static String getAttribute(Element element, String attrName) { return element.attributeValue(attrName); } public static String getText(Element element) { return element.getText(); } public static List<Element> getElements(Element element, String tagName) { return element.elements(tagName); } /** * 根据节点名称获取子节点数据 */ public static String getElementText(String resource, String qName) throws Exception { Element root = readXmlFromResources(resource).getRootElement(); String text = root.elementText(qName); return text.trim(); } /** * 根据节点名称获取子节点的子节点数据 */ public static String getElementText(String resource, String qName, String SubQName) throws Exception { Element root = readXmlFromResources(resource).getRootElement(); String text = root.element(qName).elementText(SubQName); return text.trim(); } /** * 根据节点名称获取子节点的子节点的数据列表 */ public static List<String> getElementTextList(String resource, String qName) throws Exception { List<Element> elementList = readXmlFromResources(resource).getRootElement().element(qName).elements(); List<String> list = new ArrayList<>(); for (Element e : elementList) { list.add(e.getTextTrim()); } return list; } }
@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); }
<?xml version="1.0" encoding="UTF-8"?> <mybatisGroup id="预置数据语句"> <del_1 id="del_1" type="delete"> delete from t_admin_user WHERE user_name = 'testuser'; </del_1> <del_2 id="del_2" type="delete"> delete from t_admin_user WHERE user_name = 'testuser2'; </del_2> <insert_1 id="insert_1" type="insert"> 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'); </insert_1> <del_group> <statement> delete from t_admin_user WHERE user_name = 'testuser'; </statement> <statement> delete from t_admin_user WHERE user_name = 'testuser2'; </statement> </del_group> <del_group_2> <del_1> delete from t_admin_user WHERE user_name = 'testuser'; </del_1> <del_2> delete from t_admin_user WHERE user_name = 'testuser2'; </del_2> <del_3> delete from t_admin_user WHERE user_name = 'testuser3'; </del_3> </del_group_2> </mybatisGroup>