zoukankan      html  css  js  c++  java
  • 使用SAX解析XML文件

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <Code>
     3     <SytmBankCode code="1256" name="长虹基金" logonId="6228400210568" password="123456" fieldXmlFile="changhong//browser//TestField1256B.xml">
     4         <success-case caseNo="ChangHong010on2ANC_01" runFlg="true" channelKbn="B">
     5             <action gamenID="ABC01MN000B" actionType="button" actionPath="ABC01MN000B" actionId="otoMsgeTitlInfo0" inputField="false"/>
     6             <action gamenID="ABC01MN000B" actionType="button" actionPath="ABC01MN000B" actionId="gropButn" inputField="false"/>
     7         </success-case>
     8         <success-case caseNo="ChangHong010on2ANC_02" runFlg="true" channelKbn="B">
     9             <action gamenID="ABC01MN000B" actionType="xpath" actionPath="ABC01MN000B" actionId="(//input[@type='image'])[3]" inputField="false"/>
    10             <action gamenID="ABC01MN000B" actionType="dialogButton" actionPath="ABC01MN000B" actionId="butn02" dialog="您确认要中止交易并返回主界面吗?" inputField="false"/>
    11             <action gamenID="ABC01MN000B" actionType="xpath" actionPath="ABC01MN000B" actionId="(//a[contains(text(),'返回主界面')])[2]" inputField="false"/>
    12             <action gamenID="ABC01MN000B" actionType="xpath" actionPath="ABC01MN000B" actionId="//body[@id='ABC01MN000B']/form/div[2]/div[6]/div[2]/div[8]/ul/li/a/img" inputField="false"/>
    13         </success-case>
    14     </SytmBankCode>
    15 </Code>
    TestXML.xml

    Bean文件

     1 package bean;
     2 
     3 import jp.co.hitachi_solutions.common.bean.TestBaseBean;
     4 
     5 public class TestCodeActionBean extends TestBaseBean {
     6     private String gamenID = null;
     7     private String name = null;
     8     private String actionType = null;
     9     private String actionId = null;
    10     private String waitItem = null;
    11     private String dialog = null;
    12     private String actionPath = null;
    13     private Boolean inputField = null;
    14     private Boolean screenShot = null;
    15     private int tuban = 1;
    16     
    17     public int getTuban() {
    18         return tuban;
    19     }
    20     public void setTuban(int tuban) {
    21         this.tuban = tuban;
    22     }
    23     public String getGamenID() {
    24         return gamenID;
    25     }
    26     public void setGamenID(String gamenID) {
    27         this.gamenID = gamenID;
    28     }
    29     public String getActionType() {
    30         return actionType;
    31     }
    32     public void setActionType(String actionType) {
    33         this.actionType = actionType;
    34     }
    35     public String getActionId() {
    36         return actionId;
    37     }
    38     public void setActionId(String actionId) {
    39         this.actionId = actionId;
    40     }
    41     public String getDialog() {
    42         return dialog;
    43     }
    44     public void setDialog(String dialog) {
    45         this.dialog = dialog;
    46     }
    47     public String getActionPath() {
    48         return actionPath;
    49     }
    50     public void setActionPath(String actionPath) {
    51         this.actionPath = actionPath;
    52     }
    53     public String getName() {
    54         return name;
    55     }
    56     public void setName(String name) {
    57         this.name = name;
    58     }
    59     public Boolean getInputField() {
    60         return inputField;
    61     }
    62     public void setInputField(Boolean inputField) {
    63         this.inputField = inputField;
    64     }
    65     public Boolean getScreenShot() {
    66         return screenShot;
    67     }
    68     public void setScreenShot(Boolean screenShot) {
    69         this.screenShot = screenShot;
    70     }
    71     public String getWaitItem() {
    72         return waitItem;
    73     }
    74     public void setWaitItem(String waitItem) {
    75         this.waitItem = waitItem;
    76     }
    77 }
    TestCodeActionBean.java
     1 package bean;
     2 
     3 import java.util.List;
     4 import jp.co.hitachi_solutions.common.bean.TestBaseBean;
     5 
     6 public class TestCodeSuccessCaseBean extends TestBaseBean {
     7     private Boolean runFlg = null;
     8     private String channelKbn = null;
     9     private String homePageURL = null;
    10     private List<TestCodeActionBean> actionConfig = null;
    11     public Boolean getRunFlg() {
    12         return runFlg;
    13     }
    14     public void setRunFlg(Boolean runFlg) {
    15         this.runFlg = runFlg;
    16     }
    17     public String getChannelKbn() {
    18         return channelKbn;
    19     }
    20     public void setChannelKbn(String channelKbn) {
    21         this.channelKbn = channelKbn;
    22     }
    23     public String getHomePageURL() {
    24         return homePageURL;
    25     }
    26     public void setHomePageURL(String homePageURL) {
    27         this.homePageURL = homePageURL;
    28     }
    29     public List<TestCodeActionBean> getActionConfig() {
    30         return actionConfig;
    31     }
    32     public void setActionConfig(List<TestCodeActionBean> actionConfig) {
    33         this.actionConfig = actionConfig;
    34     }
    35 }
    TestCodeSuccessCaseBean.java
     1 package bean;
     2 
     3 import java.util.List;
     4 
     5 import jp.co.hitachi_solutions.common.bean.TestBaseBean;
     6 
     7 public class TestCodeSytmBankBean extends TestBaseBean {
     8     private String bankCode = null;
     9     private String bankName = null;
    10     private String logonId = null;
    11     private String password = null;
    12     private List<TestCodeSuccessCaseBean> testCaseListBean = null;
    13 
    14     public String getBankCode() {
    15         return bankCode;
    16     }
    17     public void setBankCode(String bankCode) {
    18         this.bankCode = bankCode;
    19     }
    20     public String getBankName() {
    21         return bankName;
    22     }
    23     public void setBankName(String bankName) {
    24         this.bankName = bankName;
    25     }
    26     public String getLogonId() {
    27         return logonId;
    28     }
    29     public void setLogonId(String logonId) {
    30         this.logonId = logonId;
    31     }
    32     public String getPassword() {
    33         return password;
    34     }
    35     public void setPassword(String password) {
    36         this.password = password;
    37     }
    38     public List<TestCodeSuccessCaseBean> getTestCaseListBean() {
    39         return testCaseListBean;
    40     }
    41     public void setTestCaseListBean(List<TestCodeSuccessCaseBean> testCaseListBean) {
    42         this.testCaseListBean = testCaseListBean;
    43     }
    44 }
    TestCodeSytmBankBean.java

    解析代码:

      1 package xml;
      2 
      3 import java.io.IOException;
      4 import java.util.ArrayList;
      5 import java.util.List;
      6 
      7 import javax.xml.parsers.ParserConfigurationException;
      8 import javax.xml.parsers.SAXParser;
      9 import javax.xml.parsers.SAXParserFactory;
     10 
     11 import org.xml.sax.Attributes;
     12 import org.xml.sax.InputSource;
     13 import org.xml.sax.SAXException;
     14 import org.xml.sax.XMLReader;
     15 import org.xml.sax.helpers.DefaultHandler;
     16 
     17 import bean.TestCodeActionBean;
     18 import bean.TestCodeSuccessCaseBean;
     19 import bean.TestCodeSytmBankBean;
     20 
     21 public class SAXParserXML extends DefaultHandler {
     22 
     23     private TestCodeSytmBankBean sytmBankBean = null;
     24 
     25     private TestCodeSuccessCaseBean caseBean = null;
     26 
     27     private List<TestCodeSuccessCaseBean> caseList = null;
     28 
     29     private List<TestCodeActionBean> actionConfig = null;
     30 
     31     private String fieldXmlFile = null;
     32 
     33     public SAXParserXML(String xmlFile) {
     34         readFieldValue(xmlFile);
     35     }
     36 
     37     /*
     38      * 重写父类的startElement()方法
     39      * 接收元素开始的通知,默认情况下,不执行任何操作。
     40      * uri - 名称空间 URI,如果元素没有任何名称空间 URI,或者没有正在执行名称空间处理,则为空字符串。
     41      * localName - 本地名称(不带前缀),如果没有正在执行名称空间处理,则为空字符串。
     42      * qName - 限定的名称(带有前缀),如果限定的名称不可用,则为空字符串。
     43      * attributes - 附加到元素的属性。如果没有属性,则它将是空的 Attributes 对象。
     44      */
     45     public void startElement(String namespaceURI, String localName,
     46             String qName, Attributes attrs) throws SAXException {
     47         if ("SytmBankCode".equals(qName)) {
     48             String sytmBankCode = attrs.getValue("code");
     49             fieldXmlFile = attrs.getValue("fieldXmlFile");
     50             
     51             if (sytmBankCode != null) {
     52                 sytmBankBean = new TestCodeSytmBankBean();
     53                 
     54                 sytmBankBean.setBankCode(sytmBankCode);
     55                 sytmBankBean.setBankName(attrs.getValue("name"));
     56                 sytmBankBean.setLogonId(attrs.getValue("logonId"));
     57                 sytmBankBean.setPassword(attrs.getValue("password"));
     58                 
     59                 caseList = new ArrayList<TestCodeSuccessCaseBean>();
     60             }
     61         } else if ("success-case".equals(qName)) {
     62             caseBean = new TestCodeSuccessCaseBean();
     63             
     64             caseBean.setCaseNo(attrs.getValue("caseNo"));
     65             String tmpFlag = attrs.getValue("runFlg");
     66             caseBean.setHomePageURL(attrs.getValue("homePageURL"));
     67             if (tmpFlag != null && ("true".equals(tmpFlag) || "false".equals(tmpFlag))) {
     68                 caseBean.setRunFlg(new Boolean(tmpFlag));
     69             } else {
     70                 caseBean.setRunFlg(false);
     71             }
     72             actionConfig = new ArrayList<TestCodeActionBean>();
     73         } else if ("action".equals(qName)) {
     74             TestCodeActionBean actionConfigBean = new TestCodeActionBean();
     75             
     76             actionConfigBean.setGamenID(attrs.getValue("gamenID"));
     77             actionConfigBean.setActionType(attrs.getValue("actionType"));
     78             actionConfigBean.setActionId(attrs.getValue("actionId"));
     79             actionConfigBean.setWaitItem(attrs.getValue("waitItem"));
     80             actionConfigBean.setDialog(attrs.getValue("dialog"));
     81             actionConfigBean.setActionPath(attrs.getValue("actionPath"));
     82             String inputFlg = attrs.getValue("inputField");
     83             if (inputFlg != null && ("true".equals(inputFlg) || "false".equals(inputFlg))) {
     84                 actionConfigBean.setInputField(new Boolean(inputFlg));
     85             } else {
     86                 actionConfigBean.setInputField(false);
     87             }
     88         }
     89     }
     90 
     91     public void endElement(String uri, String localName, String qName)
     92             throws SAXException {
     93         if ("success-case".equals(qName)) {
     94             caseBean.setActionConfig(actionConfig);
     95             caseList.add(caseBean);
     96         } else if ("SytmBankCode".equals(qName)) {
     97             sytmBankBean.setTestCaseListBean(caseList);
     98         }
     99     }
    100 
    101     private void readFieldValue(String xmlFile) {
    102         try {
    103             //创建 SAXParserFactory的一个新实例以配置和获取基于SAX的解析器以解析XML文档
    104             SAXParserFactory spf = SAXParserFactory.newInstance();
    105             //指定由此代码生成的解析器将不验证被解析的文档
    106             spf.setValidating(false);
    107             //使用当前配置的工厂参数创建 SAXParser 的一个新实例
    108             SAXParser parser = spf.newSAXParser();
    109             //返回由此类的实现封装的 XMLReader
    110             XMLReader myreader = parser.getXMLReader();
    111             //注册内容事件处理程序,指定当前的类为处理器,调用startElement()和endElement()方法
    112             myreader.setContentHandler(this);
    113             //用系统标识符URI创建新的输入源
    114             InputSource source = new InputSource(xmlFile);
    115             //解析来自输入源(字符流、字节流或 URI)的 XML文档
    116             myreader.parse(source);
    117         } catch (IOException e) {
    118             e.printStackTrace();
    119         } catch (SAXException ex) {
    120             ex.printStackTrace();
    121         } catch (ParserConfigurationException pcex) {
    122             pcex.printStackTrace();
    123         }
    124     }
    125 
    126     public TestCodeSytmBankBean getSytmBankBean() {
    127         return sytmBankBean;
    128     }
    129     public String getFieldXmlFile() {
    130         return fieldXmlFile;
    131     }
    132     public void setFieldXmlFile(String fieldXmlFile) {
    133         this.fieldXmlFile = fieldXmlFile;
    134     }
    135     
    136     public static void main(String[] args){
    137         SAXParserXML sAXParserXML = new SAXParserXML("E:/xml/TestXML.xml");
    138         TestCodeSytmBankBean systemBean = sAXParserXML.getSytmBankBean();
    139         
    140         System.out.println(systemBean.getBankCode());
    141         System.out.println(systemBean.getBankName());
    142         System.out.println(systemBean.getCaseNo());
    143         System.out.println(systemBean.getLogonId());
    144         System.out.println(systemBean.getPassword());
    145         
    146         System.out.println(systemBean.getTestCaseListBean().get(0).getCaseNo());
    147     }
    148 }

    运行结果:

    1256
    长虹基金
    null
    6228400210568
    123456
    ChangHong010on2ANC_01
  • 相关阅读:
    IO 模型
    进程、线程、锁
    用多线程,实现并发,TCP
    同步锁(互斥锁),GIL锁(解释器层面的锁),死锁与递归锁
    Java项目中的常用的异常2
    JAVA项目中的常用的异常处理情况1
    添加学生信息web界面连接数据库
    jdbc下载路径
    添加学生信息 web界面 并且连接数据库
    正则表达式
  • 原文地址:https://www.cnblogs.com/luomsg/p/4049009.html
Copyright © 2011-2022 走看看