网管联盟 | 网管论坛 | 网管u家 | 网管博客 | 网管软件 | 网管求职 | 小游戏 | 网管搜索 | 网管原创 | 网管聚合 | 网管读摘 | 网管焦点 | 世界素材 | 会员投稿 | 会员中心 
中国网管联盟
Windows Linux Cisco 网络技术 数据库 黑客攻防 DotNet Java PHP 认证 新闻资讯 服务器 存储资讯 网络设备 网管学堂 技术专题 焦点 网吧频道
 当前位置: > bitsCN.com > JAVA > J2ME > 核心技术 > 分享关于j2mexmlReader的轻量级实现  

分享关于j2mexmlReader的轻量级实现

2008-05-19  作者:bitsCN整理  来源:中国网管联盟  点评 投稿 收藏


  
  四、轻量级的xmlReader
  
  幸运的是kobject.org网站上提供了一个轻量级的xmlReader程序,大家可以到http://kobjects.sourceforge.net/utils/ 获取。
  
  作为简单的xml解析器,功能上与kxml类似,但是不支持命名空间和一些传统的事件。整个jar文件大小小于5KB。
  
  以下便是其代码:
  XmlReader.java
  /* Copyright (c) 2002,2003, Stefan Haustein, Oberhausen, Rhld., Germany *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
  * in the Software without restriction, including without limitation the rights
  * to use, copy, modify, merge, publish, distribute, sublicense, and/or
  * sell copies of the Software, and to permit persons to whom the Software is
  * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * 网管u家u.bitsCN.com
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  * IN THE SOFTWARE. */package org.kobjects.xml;import java.io.*;
  import java.util.*;
  /** A minimalistic XML pull parser, similar to kXML, but
  not supporting namespaces or legacy events. If you need
  support for namespaces, or access to XML comments or
  processing instructions, please use kXML(2) instead. */public class XmlReader {
  /** Return value of getType before first call to next() */
  public final static int START_DOCUMENT = 0;
  /** Signal logical end of xml document */
网管朋友网www_bitscn_net

  public final static int END_DOCUMENT = 1;
  /** Start tag was just read */  public final static int START_TAG = 2;
  /**   * End tag was just read
  */
  public final static int END_TAG = 3;
  /** Text was just read */
  public final static int TEXT = 4;
  final static int CDSECT = 5;
  final static int ENTITY_REF = 6;
  static final private String UNEXPECTED_EOF =
  "Unexpected EOF";
  static final private int LEGACY = 999;
  // general  public boolean relaxed;
  private Hashtable entityMap;
  private int depth;
  private String[] elementStack = new String[4];
  // source  private Reader reader;
  private char[] srcBuf =
  new char[Runtime.getRuntime().freeMemory() >= 1048576
  ? 8192
  : 128];
  private int srcPos;
  private int srcCount;
  private boolean eof;
  private int line;
  private int column;
  private int peek0;
  private int peek1;
  // txtbuffer 网管u家u.bitscn@com
  private char[] txtBuf = new char[128];
  private int txtPos;
  // Event-related
  private int type;
  private String text;
  private boolean isWhitespace;
  private String name;
  private boolean degenerated;
  private int attributeCount;
  private String[] attributes = new String[16];
  private String[] TYPES =
  {
  "Start Document",
  "End Document",
  "Start Tag",
  "End Tag",
  "Text" };
  private final int read() throws IOException {
  int r = peek0;
  peek0 = peek1;
  if (peek0 == -1) {
  eof = true;
  return r;
  }    else if (r ==
    || r == ) {
  line++;
  column = 0;
  if (r == && peek0 ==
    )
  peek0 = 0;    }
  column++;
  if (srcPos >= srcCount) {
  srcCount = reader.read(srcBuf, 0, srcBuf.length);
  if (srcCount <= 0) {
  peek1 = -1;
  return r;
  }
  srcPos = 0; 网管u家www.bitscn.net
  }
  peek1 = srcBuf[srcPos++];
  return r;
  }
  private final void exception(String desc)
  throws IOException {
  throw new IOException(
  desc + " pos: " + getPositionDescription());
  }
  private final void push(int c) {
  if (c == 0)
  return;
  if (txtPos == txtBuf.length) {
  char[] bigger = new char[txtPos * 4 / 3 + 4];
  System.arraycopy(txtBuf, 0, bigger, 0, txtPos);
  txtBuf = bigger;
  }
  txtBuf[txtPos++] = (char) c;
  }  private final void read(char c) throws IOException {
  if (read() != c) {
  if (relaxed) {
  if (c <= 32) {
  skip();
  read();
  }
  }
  else {
  exception("expected: " + c + "");
  }
  }
  }
  private final void skip() throws IOException {
  while (!eof && peek0 <= )
  read();
  }
  private final String pop(int pos) {
  String result = new String(txtBuf, pos, txtPos - pos);
  txtPos = pos;
网管下载dl.bitscn.com

  return result;
  }
  private final String readName() throws IOException {
  int pos = txtPos;    int c = peek0;
  if ((c < a || c > z)
  && (c < A || c > Z)
  && c != _
  && c != :
  && !relaxed)

网管有家bitscn.net

网管u家bitscn.net

TAGs   实现   关于   分享   private   int   解析   final   xml   String   &      
 上一篇:J2ME的MVC2开源框架KBOX   下一篇:j2me任意角度翻转图片
分享关于j2mexmlReader的轻量级实现 评论:
loading.. 评论加载中…
评论:请自觉遵守互联网相关政策法规,评论不得超过250字。

验证码: 注册用户
本类热门排行:
最新推荐文章:
网管论坛交流: