Java之XML操作:从XML中直接获取数据
2021/5/22 12:25:18
本文主要是介绍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>
这篇关于Java之XML操作:从XML中直接获取数据的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南