摘要:①读取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>