J2ME中使用KXML的例子

Posted on 2005-07-25 10:17  Maverick  阅读(3202)  评论(0编辑  收藏  举报

普通的JAVA XML解析包比较大,占用资源比较多,不适合嵌入式设备使用
KXML是专门用在嵌入式设备上,只有十几K
下面是我在实际项目中用到的一代码,供大家参考
 public String XmlDecoding(DataInputStream din) throws IOException{
  String temp = null;
  Reader reader = new InputStreamReader(din);
  XmlParser parser = new XmlParser(reader);
  ParseEvent pe = null;
  parser.read(Xml.START_TAG, null, "upos");
  parser.skip();
  ParseEvent event = null;
  do {
   try {
    event = parser.read ();

    if(event.getType()==Xml.START_TAG) {
     String name = event.getName();
     temp += "<"+name + ">\n";
    }

    if(event.getType()== Xml.TEXT) {
     String name = event.getText();
     name = name.trim();
     temp += name + "\n";
    }

    if(event.getType()== Xml.END_TAG) {
     String name = event.getName();
     temp += "<"+name + ">\n";
    }

   }
   catch(IOException ex) {       
    System.out.println("Exception occured");
   }
  }
  while (event.getType() != Xml.END_DOCUMENT);
  return temp;
 }

Copyright © 2024 Maverick
Powered by .NET 9.0 on Kubernetes