Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2244322
  • 博文数量: 556
  • 博客积分: 11457
  • 博客等级: 上将
  • 技术积分: 5973
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-24 22:33
文章分类

全部博文(556)

文章存档

2013年(22)

2012年(74)

2011年(460)

分类: Java

2011-08-04 22:39:26

-----------------------------------------开始-------------------------------------------------

  1. package xml.xmlreader;
  2. import java.io.File;
  3. import java.net.URL;
  4. import java.util.Properties;
  5. import javax.xml.parsers.SAXParser;
  6. import javax.xml.parsers.SAXParserFactory;
  7. public class CFGParser {
  8.     private Properties props;

  9.     public Properties getProps() {
  10.         return props;
  11.     }
  12.     public void setProps(Properties props) {
  13.         this.props = props;
  14.     }

  15.     public void parse(String filename) throws Exception
  16.     {
  17.         CFGHandler handler = new CFGHandler();

  18.         SAXParserFactory factory = SAXParserFactory.newInstance();
  19.         factory.setNamespaceAware(false);
  20.         factory.setValidating(false);

  21.         SAXParser parser = factory.newSAXParser();

  22.         URL confURL = super.getClass().getClassLoader().getResource(filename);
  23.         if (confURL == null) {
  24.             System.out.println("Can't find configration file.");
  25.             return;
  26.         }
  27.         try
  28.         {
  29.             parser.parse(confURL.toString(), handler);
  30.             this.props = handler.getProps();
  31.         }
  32.         finally {
  33.             factory = null;
  34.             parser = null;
  35.             handler = null;
  36.         }
  37.     }

  38.     public void parseFile(String filename)
  39.     throws Exception
  40.     {
  41.         CFGHandler handler = new CFGHandler();

  42.         SAXParserFactory factory = SAXParserFactory.newInstance();
  43.         factory.setNamespaceAware(false);
  44.         factory.setValidating(false);
  45.         SAXParser parser = factory.newSAXParser();


  46.         File f = new File(filename);
  47.         if ((f == null) || (!f.exists()))
  48.             return;
  49.         try
  50.         {
  51.             parser.parse(f, handler);


  52.             this.props = handler.getProps();
  53.         }
  54.         finally {
  55.             factory = null;
  56.             parser = null;
  57.             handler = null;
  58.         }
  59.     }
  60. }
  1. package xml.xmlreader;
  2. import java.util.Properties;
  3. import org.xml.sax.Attributes;
  4. import org.xml.sax.SAXException;
  5. import org.xml.sax.helpers.DefaultHandler;

  6. public class CFGHandler extends DefaultHandler
  7. {
  8.   private Properties props;
  9.   private String currentSet;
  10.   private String currentName;
  11.   private StringBuffer currentValue = new StringBuffer();

  12.   public CFGHandler()
  13.   {
  14.     this.props = new Properties();
  15.   }

  16.   public Properties getProps() {
  17.     return this.props;
  18.   }

  19.   public void startElement(String uri, String localName, String qName, Attributes attributes)
  20.     throws SAXException
  21.   {
  22.     this.currentValue.delete(0, this.currentValue.length());
  23.     this.currentName = qName;
  24.   }

  25.   public void characters(char[] ch, int start, int length) throws SAXException
  26.   {
  27.     this.currentValue.append(ch, start, length);
  28.   }

  29.   public void endElement(String uri, String localName, String qName)
  30.     throws SAXException
  31.   {
  32.     this.props.put(qName.toLowerCase(), this.currentValue.toString().trim());
  33.   }
  34. }
  1. xml文件


  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <xml-body>
  4.         <refresh_userlist desc="用户列表刷新间隔时间(秒)">6</refresh_userlist>
  5.         <refresh_message desc="短消息刷新间隔时间(秒)">10</refresh_message>
  6.         <morningbegin desc="上午上班时间">23:00</morningbegin>
  7.         <morningend desc="上午下班时间">12:00</morningend>
  8.         <afternoonbegin desc="下午上班时间">18:00</afternoonbegin>
  9. </xml-body>
jsp获取各个节点的值:
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <html>
  3.     <jsp:useBean id="cfgp" scope="page" class="xml.xmlreader.CFGParser"></jsp:useBean>
  4.     <body>
  5.         <%
  6.    cfgp.parse("kaoqin.xml");
  7.    Properties pro = cfgp.getProps();
  8.    String stTime = pro.getProperty("morningbegin");
  9.    String edTime = pro.getProperty("morningend");
  10.     String afternoonbegin = pro.getProperty("afternoonbegin");
  11.    
  12.    out.println(stTime+"\n"+edTime+"\n"+afternoonbegin);
  13.    System.out.println(stTime+"\n"+edTime+"\n"+afternoonbegin);
  14.     %>
  15.     </body>
  16. </html>
转自:http://blog.163.com/lujun19888@126/blog/static/3082197220103304016296/
 
阅读(3986) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~