zoukankan      html  css  js  c++  java
  • XML读写工具类

    摘要:①读取XML文件,生成pojo对象;②将对象信息保存到xml中。

    步骤:

    ①新建一个普通的java类BasePage;

    package com.test.selenium.pages;
    
    import java.util.Map;
    
    import com.thoughtworks.xstream.annotations.XStreamAlias;
    
    
    @XStreamAlias("basePage")
    public class BasePage {
        @XStreamAlias("pageName")
        private String pageName;
        
        @XStreamAlias("pageXpaths")
        private Map<String, String> pageXpaths;
    
        public BasePage() {}
    
        public BasePage(String pageName, Map<String, String> pageXpaths) {
            super();
            this.pageName = pageName;
            this.pageXpaths = pageXpaths;
        }
        
        public String getPageXpath(String name) {
            return pageXpaths.get(name);
        }
    
        public String getPageName() {
            return pageName;
        }
    
        public void setPageName(String pageName) {
            this.pageName = pageName;
        }
    
        public Map<String, String> getPageXpaths() {
            return pageXpaths;
        }
    
        public void setPageXpaths(Map<String, String> pageXpaths) {
            this.pageXpaths = pageXpaths;
        }
    
    }

    ②新建一个page.xml;

    <basePage>
      <pageName>pageName</pageName>
      <pageXpaths>
        <entry>
          <string>name2</string>
          <string>value2</string>
        </entry>
        <entry>
          <string>name1</string>
          <string>value1</string>
        </entry>
      </pageXpaths>
    </basePage>

    ③封装XMLTools类;

    package com.test.selenium.utils;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    
    import com.thoughtworks.xstream.XStream;
    import com.thoughtworks.xstream.annotations.XStreamAlias;
    import com.thoughtworks.xstream.io.xml.DomDriver;
    
    public class XMLTools {
    
        /**
         * 将对象信息保存到xml中
         * 
         * @param entity
         * @param file
         */
        public static <T> void saveToXml(T entity, String file) throws Exception {
    
            XStream stream = new XStream(new DomDriver("utf-8"));
            stream.autodetectAnnotations(true);
    
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            stream.toXML(entity, fileOutputStream);
        }
    
        /**
         * 从配置文件中读取配置,并自动转换为对应的对象
         * 
         * @param clazz
         * @param file
         * @return 对象
         * @throws Exception
         */
        @SuppressWarnings({ "unchecked" })
        public static <T> T getFromXml(Class<T> clazz, String file) throws Exception {
    
            XStream xStream = new XStream(new DomDriver("utf-8"));
            xStream.autodetectAnnotations(true);
            FileInputStream fileInputStream = null;
            T entity = null;
    
            fileInputStream = new FileInputStream(file);
            String alias = "";
    
            if (clazz.isAnnotationPresent(XStreamAlias.class)) {
                alias = clazz.getAnnotation(XStreamAlias.class).value();//获取POJO里面的@XStreamAlias()的值
            }
    
            xStream.alias(alias, clazz);
    
            entity = (T) xStream.fromXML(fileInputStream);
    
            return entity;
        }
    
    }

    ④工具类XMLTools的测试类;

    package com.test.selenium.test;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.junit.Test;
    
    import com.test.selenium.pages.BasePage;
    import com.test.selenium.utils.XMLTools;
    
    public class XMLToolsTest {
    
        @Test
        public void testGetPage() {
            BasePage basePage = null;
            try {
                basePage = XMLTools.getFromXml(BasePage.class, "pages/page.xml");
                System.out.println(basePage.getPageName());
                for (Map.Entry<String, String> path:basePage.getPageXpaths().entrySet()){
                    System.out.println(path.getKey());
                    System.out.println(path.getValue());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        @Test
        public void testSavePage(){
            Map<String, String> paths = new HashMap<>();
            paths.put("name1", "value1");
            paths.put("name2", "value2");
            
            BasePage basePage = new BasePage("pageName", paths);
            
            try {
                XMLTools.saveToXml(basePage, "pages/page2.xml");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    }

    ⑤运行结果;

    --testGetPage:

    pageName
    name2
    value2
    name1
    value1

    --testSavePage:

    <!--page2.xml文件-->
    <basePage>
      <pageName>pageName</pageName>
      <pageXpaths>
        <entry>
          <string>name2</string>
          <string>value2</string>
        </entry>
        <entry>
          <string>name1</string>
          <string>value1</string>
        </entry>
      </pageXpaths>
    </basePage>
  • 相关阅读:
    4 Apr 18 软件开发目录 logging模块的使用 序列化(Json, Pickle) os模块
    3 Apr 18 内置函数 列表生成式与生成器表达式 模块的使用之import 模块的使用之from…import…
    2 Apr 18 三元表达式 函数递归 匿名函数 内置函数
    30 Mar 18 迭代器 生成器 面向过程的编程
    29 Mar 18 函数 有参、无参装饰器
    28 Mar 18 函数
    27 Mar 18 函数的参数
    26 Mar 18 函数介绍
    23 Mar 18 文件处理
    22 Mar 18 补充数据类型+字符编码+文件处理
  • 原文地址:https://www.cnblogs.com/yigui/p/7651918.html
Copyright © 2011-2022 走看看