⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 saxparser1.java

📁 一个java的解析xml的例子
💻 JAVA
字号:
package xmlsax;import java.util.*;import java.io.*;import java.io.IOException;import org.xml.sax.*;import org.xml.sax.helpers.*;import org.apache.xerces.parsers.SAXParser;public class SaxParser1 extends DefaultHandler {  private java.util.Stack  tagStack=new java.util.Stack();  private String resultXML="";  //写成XML的字符串  private String indent="";  private static final java.lang.String NEWLINE=System.getProperty("line.separator");  private PrintWriter printFile=null;  private String resultPZ="您所选择的设备类型如下:" + NEWLINE;   //返回所选择的设备类型  private String resultTXT="";//写成文本的字符串  String[] res =new String[10];       //类型列表  int len = 0,sel,selport,f=0;  String sbname,tag;  public static void main(String[] argv) {    if (argv.length != 1) {      System.out.println("Usage: java SaxParser1 [URI]");      System.exit(0);    }    String uri = argv[0];    try {      XMLReader parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");      SaxParser1 SaxParser1Instance = new SaxParser1();      parser.setContentHandler(SaxParser1Instance);      parser.parse(uri);    }    catch(IOException ioe) {      ioe.printStackTrace();    }    catch(SAXException saxe) {      saxe.printStackTrace();    }  }//将字符串中的单词拆开  public String[] sTOc(String temp){    String[] res =new String[10];    StringTokenizer st = new StringTokenizer(temp);    len = 0;    while(st.hasMoreTokens())      res[len++] = st.nextToken() + " " + sbname;    return res;  }//显示外设选择菜单  private int selectSb(String[] sb) {    int i;    String s = "";    String ask = "\n确认请按回车,或选择其它";    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));    System.out.println("请在下列" + sbname + "类型中选择一种\n");    for(i=0;i<len;i++)      System.out.println("        " + i +"."+ sb[i] + "\n");    i=0;    System.out.println("请输入0 -- " + (len-1) + "(直接回车,默认选择0):");    try {      s = in.readLine();      Integer tt=new java.lang.Integer(0);      while ( s.length() != 0) {      	i=tt.valueOf(s).intValue();        if(i<len && i>=0)            System.out.println("您选择的" + sbname + "是: " + sb[i] + "\n" + ask + sbname);        else            System.out.println("错误的选择!!");        s = in.readLine();      }    } catch (IOException e) {        e.printStackTrace();      }    return i;  }//显示所用端口选择菜单  private int selectPort(String sbname) {    int i;    String s = "";    String ask = "\n确认请按回车,或选择其它";    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));    System.out.println("请为" + sbname + "选择一种辅口\n");    i=1;    System.out.println("请输入1 或 2 或 3 分别代表辅口1、2、3(直接回车,默认选择1):");    try {      s = in.readLine();      Integer tt=new java.lang.Integer(0);      while ( s.length() != 0) {      	i=tt.valueOf(s).intValue();        if(i<3 && i>=0)            System.out.println("您为" + sbname + "设备选择的辅口是: " + i + "\n" + ask + sbname);        else            System.out.println("错误的选择!!");        s = in.readLine();      }    } catch (IOException e) {        e.printStackTrace();      }    return i;  }  private String locateValue(String tagName) {    return sbname;  }    /**@todo: Implement this characters method*/  public void characters(char[] ch, int start, int length) throws SAXException {    String value = new String(ch, start, length);    if(f == 1){      resultTXT=resultTXT+value+"}"+NEWLINE;      f = 0;    }  }    /**@todo: Implement this endDocument method*/  public void endDocument() throws SAXException {    String ask = "\n确认请按回车,或输入其它文件名:";    String s = "";    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));    String filename = "config.ini";    System.out.println(resultPZ);    System.out.println("您是否将上面的选择保存为配置文件Y/N(直接回车,默认选择N):");    try {      s = in.readLine();      if ( s.length() != 0) {        if(s.equals("Y")|| s.equals("y")){            System.out.println("您是否保存为:" + filename + "\n" + ask);            s = in.readLine();           if ( s.length() != 0)              filename = s;            try {                printFile=new PrintWriter(new FileWriter(filename));                printFile.print(resultTXT);                printFile.close();                System.out.println("您已成功保存配置文件:" + filename);            } catch(IOException e) {                System.out.println("打开 " + filename + " 时出错:"+e);            }        }      }    } catch (IOException e) {        e.printStackTrace();      }  }    /**@todo: Implement this endElement method*/  public void endElement(String uri, String localName, String qName) throws SAXException {    if(localName.equals("trnDef")) {	}	else if(localName.equals("Device")) {		String tagName=(String)tagStack.pop();		indent=indent.substring(0,indent.length()-4);                  resultXML=resultXML+indent+"</"+tagName+">"+NEWLINE;	}	else if(localName.equals("Name")) {		String tagName=(String)tagStack.pop();		indent=indent.substring(0,indent.length()-4);                if (res[sel].equals(tag))                resultXML=resultXML+indent+"</"+tagName+">"+NEWLINE;              else                tag = "";	}  }    /**@todo: Implement this startDocument method*/  public void startDocument() throws SAXException {  }    /**@todo: Implement this startElement method*/  public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {    if(localName.equals("Def")) {	}	else if(localName.equals("Device")) {		String tagName=attributes.getValue(0);                String tagValue=attributes.getValue(1);                sbname = tagName;                res=sTOc(tagValue);                sel=selectSb(res);                if (tagName.equals("密码键盘") || tagName.equals("读卡器"))                   selport=selectPort(tagName);        	tagStack.push(tagName);                tag = tagName + " " + sbname;                resultXML=resultXML+indent+"<"+tagName+">"+NEWLINE;		indent+="    ";                resultPZ=resultPZ+tagName+"=";      //返回设备名                resultTXT=resultTXT+"<"+tagName+"=";      //返回设备名	}	else if(localName.equals("Name")) {		String tagName=attributes.getValue(0);         	tagStack.push(tagName);                tag = tagName + " " + sbname;                if (res[sel].equals(tag)){                  resultXML=resultXML+indent+"<"+tagName+">"+NEWLINE;                  resultPZ=resultPZ+tagName+NEWLINE;  //返回设备类型                  resultTXT=resultTXT+tagName+">"+NEWLINE;  //返回设备类型                if (sbname.equals("读卡器")){                  resultTXT=resultTXT+"{MSR_PORT="+selport+"}"+NEWLINE;                  resultPZ=resultPZ+"MSR_PORT="+selport+NEWLINE;  //返回设备类型                 }else                 if(sbname.equals("密码键盘")){                  resultTXT=resultTXT+"{PINPAD_PORT="+selport+"}"+NEWLINE;                  resultPZ=resultPZ+"PINPAD_PORT="+selport+NEWLINE;  //返回设备类型                 }               }		indent+="    ";	}	else if(localName.equals("Values")) {		String tagName=attributes.getValue(0);		String tagValue=locateValue(tagName);		if(!tagValue.equals("")) {                if (res[sel].equals(tag)){                  resultXML=resultXML+indent+"<"+tagName+">"+tagValue+"</"+tagName+">"+NEWLINE; //                 resultTXT=resultTXT+"{"+tagName+"="+tagValue+"}"+NEWLINE;                  resultTXT=resultTXT+"{"+tagName+"=";                  f = 1;                }		}	}  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -