java读取xml文件

上一篇 / 下一篇  2014-07-10 15:28:51 / 个人分类:selenium

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;


import org.openqa.selenium.WebDriver;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class BaseTestCase {
 public WebDriver driver;

 public static Map ReadXml(String path) throws ParserConfigurationException,
    IOException, SAXException {
  Map map = new LinkedHashMap();
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder = dbf.newDocumentBuilder();
  Properties properties = System.getProperties();
  String stream = properties.getProperty("user.dir") + path;
  File f = new File(stream);
  if (f.exists()) {
   Document doc = builder.parse(f);
   Element root = (Element) doc.getDocumentElement();
   NodeList list = root.getElementsByTagName("dictionary");
   for (int i = 0; i < list.getLength(); i++) {
    String key = list.item(i).getAttributes().getNamedItem("key").getNodeValue();
    String value = list.item(i).getAttributes().getNamedItem("value").getNodeValue();
    map.put(key, value);
   }
  }
  return map;
 }
 } 
/////////////////////////////////////////////////////////////////////////
import static org.junit.Assert.*; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.LinkedHashMap; 
import java.util.Map; 
import java.util.Properties; 
import javax.xml.parsers.ParserConfigurationException; 
import org.junit.After; 
import org.junit.AfterClass; 
import org.junit.Before; 
import org.junit.BeforeClass; 
import org.junit.Test; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.xml.sax.SAXException; 

/** 
* @author yang.zhou 

*/ 
public class Demotest extends BaseTestCase { 
public WebDriver driver; 

@Before 
public void setUp() throws Exception { 

driver = new FirefoxDriver(); 
// System.setProperty("webdriver.firefox.bin","C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"); 


/** 
* @throws java.lang.Exception 
*/ 
@After 
public void tearDown() throws Exception { 
driver.quit(); 


@Test 
public void test() throws ParserConfigurationException, SAXException, 
IOException, InterruptedException { 
driver.get(""); 
Map map = ReadXml("\\DataXml\\Online.xml"); 
driver.findElement(By.id("DepartCity1TextBox")).clear(); 
Thread.sleep(500); 
driver.findElement(By.id("DepartCity1TextBox")).sendKeys(map.get("出发城市").toString()); 
Thread.sleep(1000); 


TAG:

 

评分:0

我来说两句

日历

« 2024-05-01  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 29373
  • 日志数: 27
  • 建立时间: 2014-03-18
  • 更新时间: 2014-07-10

RSS订阅

Open Toolbar