码迷,mamicode.com
首页 > 其他好文 > 详细

dom4j解析xml作为测试数据

时间:2015-09-09 12:51:18      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

构造函数:

public AppTestData(File xmlFile) throws Exception {
        file = xmlFile;

        try {
            saxReader = new SAXReader();
            saxReader.setEncoding("UTF-8");
            doc = saxReader.read(this.file);
            
            init();
        } catch (Exception ex) {
            throw new Exception("Open xml file " + xmlFile.getAbsolutePath()
                    + " failed with error:" + ex.getMessage());
        }        
    }

初始化方法:

private void init() {
        // =========Set account data=========
        Node node = doc.selectSingleNode("//Account/User1");
        sUser1 = node.valueOf("@User");
        sUserName1 = node.valueOf("@Name");
        sPwd1 = node.valueOf("@PWD");
        sJGTUser1 = node.valueOf("@JGT");
        sJGTName1 = node.valueOf("@JGTName");
        sJGTPwd1 = node.valueOf("@JGTPWD");
        sJGTTSFPWD1 = node.valueOf("@JGTTSFPWD");
        sHarborName1 = node.valueOf("@hName");
        sDomain1 = node.valueOf("@domain");
}

xml数据文件:

<?xml version="1.0" encoding="utf-8" ?>
<TestData>
  <Account>
        <User1 User="test16" Name="Auto16" PWD="123456abc" JGT="" JGTName="ssss123456" JGTPWD="888@@@" JGTTSFPWD="888@@@" hName="Auto-User1" domain="AutoUser1" desc="" />
        <User2 User="test30" Name="AutoTest" PWD="123456abc" JGT="" JGTName="ssss123456" JGTPWD="888@@@" JGTTSFPWD="888@@@" hName="Auto-User2" domain="AutoUser2" desc="" />
</Account>
</TestData>

 

dom4j解析xml作为测试数据

标签:

原文地址:http://www.cnblogs.com/lgm1999/p/4794041.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!