SAXParseIPInfo.java~65~
资源名称:Java.rar [点击查看]
上传用户:liming9091
上传日期:2014-10-27
资源大小:3376k
文件大小:3k
源码类别:
Java编程
开发平台:
Java
- package tsinghuaip;
- import org.xml.sax.*;
- import org.xml.sax.helpers.*;
- import javax.xml.parsers.SAXParserFactory;
- import javax.xml.parsers.SAXParser;
- import java.util.*;
- public class SAXParseIPInfo {
- private Vector startIPVector;
- private Vector endIPVector;
- private Vector localVector;
- public SAXParseIPInfo() {
- startIPVector = new Vector();
- endIPVector = new Vector();
- localVector = new Vector();
- }
- public void SetStartIPVector(Vector vector) {
- this.startIPVector = vector;
- }
- public void SetEndIPVector(Vector vector) {
- this.endIPVector = vector;
- }
- public void SetLocalVector(Vector vector) {
- this.localVector = vector;
- }
- public Vector GetStartIPVector() {
- return this.startIPVector;
- }
- public Vector GetEndIPVector() {
- return this.endIPVector;
- }
- public Vector GetLocalVector() {
- return this.localVector;
- }
- public void ParseIPInfo() throws Exception {
- SAXParserFactory spf = SAXParserFactory.newInstance();
- SAXParser parser = spf.newSAXParser();
- XMLReader xmlReader = parser.getXMLReader();
- IPInfoSAXHandler ipInfoHandler = new IPInfoSAXHandler();
- xmlReader.setContentHandler(ipInfoHandler);
- InputSource source = new InputSource("TsinghuaIPAssign.xml");
- xmlReader.parse(source);
- startIPVector = ipInfoHandler.GetStartIPVector();
- endIPVector = ipInfoHandler.GetEndIPVector();
- localVector = ipInfoHandler.GetLocalVector();
- }
- }
- class IPInfoSAXHandler extends DefaultHandler {
- private String strElementName;
- private Vector startIPVector;
- private Vector endIPVector;
- private Vector localVector;
- public IPInfoSAXHandler() {
- startIPVector = new Vector();
- endIPVector = new Vector();
- localVector = new Vector();
- }
- public void characters(char ch[], int start, int length) throws
- SAXException {
- String strData = new String(ch, start, length).trim();
- if (strData.length() == 0)
- return;
- try {
- // byte[] bytes = strData.getBytes();
- // strData = new String(bytes,"GB2312");
- strData = new String(strData.getBytes("GB2312"));
- if (this.strElementName.indexOf("STARTIP") >= 0)
- startIPVector.addElement(strData);
- if (this.strElementName.indexOf("ENDIP") >= 0)
- endIPVector.addElement(strData);
- if (this.strElementName.indexOf("LOCAL") >= 0)
- localVector.addElement(strData);
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void startElement(String uri, String localName, String qName,
- Attributes atts) throws SAXException {
- this.strElementName = qName;
- }
- public void endDocument() throws SAXException {
- }
- public Vector GetStartIPVector() {
- return this.startIPVector;
- }
- public Vector GetEndIPVector() {
- return this.endIPVector;
- }
- public Vector GetLocalVector() {
- return this.localVector;
- }
- }