• 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>
  • 相关阅读:
    解题:HNOI 2008 玩具装箱
    2016级算法第一次上机助教版解题报告
    求解斐波那契数列复杂度分析
    数据库复习之规范化理论应用(第八次上机内容)
    数据库复习之规范化理论
    题目1042:Coincidence(最长公共子序列)
    题目1020:最小长方形(简单)
    题目1016:火星A+B(字符串拆分)
    题目1014:排名(结构体排序)
    题目1021:统计字符(hash简单应用)
  • 原文地址:https://www.cnblogs.com/sxw123/p/14798393.html
Copyright © 2020-2023  润新知