无花的空间--http://wuhua.3geye.net-- 您今天UCWEB了吗?--http://www.ucweb.com

当你来到我的空间的时候,你会看到很多Google,Nokia,Mobile,J2ME ,Android,3G,无线开发,手机软件,手机软件开发等与Google,手机相关的文章,你千万不要惊讶。这就是我一个专注Google,无线开发的无花。传播知识,分享经验,技术交流是我开空间的直接目的。当然如果有项目开发,我还是很希望赚点外快的。

用户操作
[即时聊天] [发私信] [加为好友]
无花ID:gooogledev
40542次访问,排名2971,好友20人,关注者24人。
J2EE 2年的开发时间,
J2ME 2年的开发时间
gooogledev的文章
原创 80 篇
翻译 0 篇
转载 3 篇
评论 201 篇
无花的公告

人要吃很多苦才可以长大!

踏实平凡人应该遵守的原则!

与我TM

gooogledev@gmail.com

友情链接

我的j2me创意

Wap浏览器的源代码

用自己写的rms引擎写的电话本

RSS订阅此博客  
用抓虾订阅此博客
用google订阅此博客
用bloglines订阅此博客
    网络封神榜 | 饶荣庆
最近评论
22:main()在哪个.java中
ant-shopping:I support author's viewpoint, hoped that will have later also more better articles,
abercrombie fitch handbag
ptsell:I support author's viewpoint, hoped that will have later also more better articles,
Balenciaga Bags
真的假的,屁股上的痣也能拍出来?
leo2012:奇怪,怎么下的wap explorer不能访问wap站点
文章分类
收藏
    相册
    j2me创意图片
    j2me教程
    我的照片
    我的链接
    3G视线
    EasyMF J2ME框架
    Lucene中国
    我的Google
    无花的博客
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 一个简单的XML for J2ME收藏

    新一篇: 我的第一个Android 多媒体的Demo | 旧一篇: 探讨J2ME 流媒体的实现

    作者:wuhua
    空间:htt://wuhua.3geye.net
    转载请保留上面的信息(请尊重知识产品)谢谢

     

    相信大家都用过Kxml的东西了,不过个人感觉kxml还是大了些。现在介绍一个比kxml跟简介的xml的类。对于一些小项目,或者对xml解释要求不是很高的项目来说却是个不错的选择。

    下面看看那代码与Demo吧。

    Java代码 复制代码
    1. package org.gggeye.easymf.xml;  
    2.   
    3. import java.util.Enumeration;  
    4. import java.util.Vector;  
    5.   
    6. /** 
    7.  *  
    8.  * @author wuhua 
    9.  * <a href="http://wuhua.3geye.net">我的博客</a> 
    10.  * 
    11.  */  
    12.    
    13. public class XMLParser {  
    14.     private XMLNode root=null;  
    15.     private XMLNode exeNode=null;  
    16.     private int offset=0;  
    17.     private String xml="";  
    18.     private int xmlLength=0;  
    19.     private String version="1.1";  
    20.     private String encoding="UTF-8";  
    21.     public XMLParser(String xml){  
    22.         this.xml=xml;  
    23.         this.xmlLength=xml.length();  
    24.     }  
    25.     public char getNextCharacter(){  
    26.         char rt= xml.charAt(offset);  
    27.         offset++;  
    28.         return rt;  
    29.           
    30.     }  
    31.     /** *//** 
    32.      * 判断下一字符是否为指定字符token 
    33.      * @param token 
    34.      * @return 
    35.      */  
    36.     private boolean match(char token){  
    37.         for(int i=offset;i<this.xmlLength;i++){  
    38.             char tc=xml.charAt(i);  
    39.             if (tc!=' '){  
    40.                 if (tc==token){  
    41.                    return true;  
    42.                 }else{  
    43.                    return false;  
    44.                 }  
    45.             }  
    46.         }  
    47.         return false;  
    48.     }  
    49.       
    50.     private String getDescription(){  
    51.         skipSpace();  
    52.         StringBuffer desc=new StringBuffer();  
    53.         while(offset<this.xmlLength-2){  
    54.             char tc1=this.getNextCharacter();  
    55.             if (tc1=='-'){  
    56.                 if ((xml.charAt(offset)=='-')&&(xml.charAt(offset+1)=='>')){  
    57.                     offset+=2;  
    58.                     return desc.toString();  
    59.                 }      
    60.             }else{  
    61.                 desc.append(tc1);  
    62.             }  
    63.         }  
    64.         return null;  
    65.           
    66.     }  
    67.     /** *//** 
    68.      * 获取Node名称 
    69.      * @return 
    70.      */  
    71.     private String getNodeName(){  
    72.         skipSpace();  
    73.         char[] name=new char[120];//   
    74.         int i=0;  
    75.         while(i<120){  
    76.             char tc=getNextCharacter();  
    77.             if ((tc==' ')||(tc=='>')||(tc=='/')){  
    78.                 if (i>0)  
    79.                     return new String(name).trim();  
    80.             }else {     
    81.                 name[i]=tc;  
    82.                 i++;  
    83.                 if (i>120){  
    84.                     System.err.println("NODE NAME长度只能小于120");  
    85.                     return null;  
    86.                 }  
    87.             }  
    88.         }  
    89.         return null;  
    90.     }  
    91.       
    92.     /** *//** 
    93.      * 获取属性信息 
    94.      * 
    95.      */  
    96.     private void getAttributes(){  
    97.         skipSpace();  
    98.         StringBuffer name=new StringBuffer();  
    99.         StringBuffer value=new StringBuffer();  
    100.         boolean isAttribute=false;  
    101.         while(offset<this.xmlLength){  
    102.             char tc1=this.getNextCharacter();  
    103.             if (tc1=='='){  
    104.                 skipSpace();  
    105.                 char tc2=this.getNextCharacter();  
    106.                 if (tc2=='"'){//获取属性值  
    107.                     isAttribute=true;  
    108.                     while(offset<this.xmlLength){  
    109.                         char tc3=this.getNextCharacter();  
    110.                         if (tc3=='"'){  
    111.                             this.exeNode.setAttribute(name.toString(),value.toString());  
    112.                             this.skipSpace();  
    113.                             value.delete(0,value.length());  
    114.                             name.delete(0,name.length());  
    115.                             break;  
    116.                         }else  
    117.                             value.append(tc3);  
    118.                     }  
    119.                 }      
    120.             }else if (tc1=='/'){  
    121.                 skipSpace();  
    122.                 char tc2=this.getNextCharacter();  
    123.                 if (tc2!='>'){  
    124.                     System.err.println("/必须使用>来封闭");  
    125.                 }else{  
    126.                     this.exeNode=this.exeNode.getParent();  
    127.                     break;  
    128.                 }  
    129.             }else if (tc1=='>'){  
    130.                 break;  
    131.             }else{  
    132.                 name.append(tc1);  
    133.             }  
    134.         }          
    135.     }  
    136.   
    137.     private int skipSpace(){  
    138.         int skipCount=0;  
    139.         while(offset<xml.length()){  
    140.             char tc=xml.charAt(offset);  
    141.             if ((tc!=' ')&&(tc!=' ')&&(tc!=' ')){  
    142.                 return skipCount;  
    143.             }else{  
    144.                 offset++;  
    145.                 skipCount++;  
    146.             }  
    147.         }  
    148.         return skipCount;  
    149.     }  
    150.     private String getValue(){  
    151.         StringBuffer value=new StringBuffer();  
    152.         value.append(xml.charAt(offset-1));  
    153.         while(offset<xml.length()){  
    154.             char tc=this.getNextCharacter();  
    155.             value.append(tc);  
    156.             if (xml.charAt(offset)=='<'){  
    157.                 return value.toString().trim();  
    158.             }  
    159.         }  
    160.         return null;          
    161.     }  
    162.     private void getXMLHeader(){  
    163.         this.skipSpace();  
    164.         if ((this.xml.charAt(offset)=='<')&&(this.xml.charAt(offset+1)=='?')){  
    165.                  int idx=this.xml.indexOf("version");  
    166.                  if (idx>0){  
    167.                      boolean start=false;  
    168.                      StringBuffer tmp=new StringBuffer();  
    169.                      for(int i=idx+8;i<this.xmlLength;i++){  
    170.                          char tc=this.xml.charAt(i);  
    171.                           if (tc=='"'){  
    172.                               if (start==false){  
    173.                                   start=true;  
    174.                               }else{  
    175.                                   break;  
    176.                               }  
    177.                           }else{  
    178.                               if (start)  
    179.                                  tmp.append(tc);  
    180.                           }  
    181.                      }  
    182.                      this.version=tmp.toString();  
    183.                        
    184.                  }  
    185.                  idx=this.xml.indexOf("encoding");  
    186.                  if (idx>0){  
    187.                      boolean start=false;  
    188.                      StringBuffer tmp=new StringBuffer();  
    189.                      for(int i=idx+9;i<this.xmlLength;i++){  
    190.                         char tc=this.xml.charAt(i);  
    191.                          if (tc=='"'){  
    192.                             if (start==false){  
    193.                                 start=true;  
    194.                             }else{  
    195.                                 break;  
    196.                             }  
    197.                          }else{  
    198.                             if (start)  
    199.                                tmp.append(tc);  
    200.                          }  
    201.                      }  
    202.                      this.encoding=tmp.toString();  
    203.                  }  
    204.                  int end=this.xml.indexOf("?>");  
    205.                  offset=end+2;  
    206.             }      
    207.               
    208.     }  
    209.     public XMLNode parse(){  
    210.         getXMLHeader();  
    211.         while(offset<this.xmlLength){  
    212.             this.skipSpace();  
    213.             char token=getNextCharacter();  
    214.             if (token=='<'){  
    215.               if (match('!')){  
    216.                   getNextCharacter();  
    217.                      char tc=getNextCharacter();  
    218.                      if (tc=='-'){  
    219.                         tc=getNextCharacter();  
    220.                         if (tc=='-'){  
    221.                            //System.out.println("注释行");  
    222.                            String desc=getDescription();  
    223.                            if (this.exeNode!=null)  
    224.                             this.exeNode.setDescription(desc);  
    225.                         }else{  
    226.                            System.err.println("语法错误在"+offset);     
    227.                            return null;  
    228.                         }     
    229.                      }   
    230.                }else if (match('/')){  
    231.                      String nodeName=this.getNodeName();  
    232.                      if (exeNode.getName().equalsIgnoreCase(nodeName))  
    233.                            exeNode=exeNode.getParent();  
    234.                      else{  
    235.                            System.err.println("期望封闭标签为:"+exeNode.getName()+",实际标签为:"+nodeName);  
    236.                            return null;                      
    237.                      }  
    238.                }else{  
    239.                   String name=this.getNodeName();  
    240.                   XMLNode newNode=new XMLNode(name);  
    241.                   if (root==null){  
    242.                      root=newNode;  
    243.                      exeNode=root;  
    244.                   }else{  
    245.                      exeNode.addChild(newNode);  
    246.                      exeNode=newNode;  
    247.                   }  
    248.                   char tc=this.xml.charAt(offset-1);  
    249.                   if (tc==' ')  
    250.                        getAttributes();  
    251.                   else{  
    252.                        if (tc!='>')  
    253.                           System.err.println(exeNode.getName()+"期待关闭");  
    254.                   }  
    255.                }  
    256.             }else{  
    257.                 exeNode.setValue(getValue());  
    258.             }  
    259.         }  
    260.         return root;  
    261.               
    262.     }  
    263.     public static void main(String[] args){  
    264.         String xml="<?xml version=\"1.0\" encoding=\"GB2312\"?>" +  
    265.                 "<!--注释行--><root desc=\"一个测试的例子\"><book name=\"test\" " +  
    266.                 "value=\"我的\"/><book name=\"跌而\">我的值</book></root>";  
    267.         XMLParser parser=new XMLParser(xml);  
    268.         XMLNode root=parser.parse();  
    269.         System.out.println(root.getName());  
    270.           
    271.         Vector nodes = root.getChildNodes();  
    272.         load(nodes);  
    273.           
    274.           
    275.        // System.out.println(root.toString());  
    276.     }  
    277.       
    278.     static void load(Vector _nodes){  
    279.            
    280.          System.out.println(_nodes);  
    281.            
    282.          for(int i=0; i<_nodes.size(); i++){  
    283.              XMLNode tNode = (XMLNode) _nodes.elementAt(i);  
    284.              System.out.println(tNode.getName());  
    285.              Enumeration keys=tNode.getAttributes().keys();  
    286.                 while(keys.hasMoreElements()){  
    287.                     String key=(String)keys.nextElement();  
    288.                       
    289.                     String value= tNode.getAttribute(key);  
    290.                     System.out.println(" "+ key+ "=" + value + " ");  
    291.                 }  
    292.                 System.out.println(tNode.getValue());  
    293.          }  
    294.     }  
    295. }  

     

    Java代码 复制代码
    1. package org.gggeye.easymf.xml;  
    2.   
    3. import java.util.Enumeration;  
    4. import java.util.Hashtable;  
    5. import java.util.Vector;  
    6.   
    7.   
    8.   
    9. /** 
    10.  * XML Node  
    11.  * @author wuhua 
    12.  * <a href="http://wuhua.3geye.net">我的博客</a> 
    13.  * 
    14.  */  
    15.   
    16. public class XMLNode {  
    17.     private String name;  
    18.     private XMLNode parent;  
    19.     private String value;  
    20.     private String description;  
    21.     private Hashtable attributes=new Hashtable();  
    22.     private Vector childNodes=new Vector();  
    23.     public XMLNode(String name){  
    24.         this.name=name;  
    25.     }  
    26.       
    27.     public String getName(){  
    28.         return this.name;  
    29.     }  
    30.     public void setName(String name){  
    31.         this.name=name;  
    32.     }  
    33.     public String getValue(){  
    34.         return this.value;  
    35.     }  
    36.     public void setValue(String value){  
    37.         this.value=value;  
    38.     }  
    39.     public void setAttribute(String name,String value){  
    40.         this.attributes.put(name,value);  
    41.     }  
    42.     public String getAttribute(String attributeName){  
    43.         return (String)attributes.get(attributeName);  
    44.     }  
    45.     public void setDescription(String desc){  
    46.         this.description=desc;  
    47.     }  
    48.     public String getDescription(){  
    49.         return this.description;  
    50.     }  
    51.     public void setParent(XMLNode parent){  
    52.         this.parent=parent;  
    53.     }  
    54.     public XMLNode getParent(){  
    55.         return this.parent;  
    56.     }  
    57.     public void addChild(XMLNode childNode){  
    58.         this.childNodes.addElement(childNode);  
    59.         childNode.setParent(this);   
    60.     }  
    61.       
    62.     public String toString(){  
    63.         StringBuffer xml=new StringBuffer();  
    64.         if ((this.getDescription()!=null)&&(this.getDescription().length()>0))  
    65.            xml.append("<!--"+this.getDescription()+"--> ");  
    66.         xml.append("<");  
    67.         xml.append(this.getName());  
    68.           
    69.         Enumeration keys=this.attributes.elements();  
    70.         while(keys.hasMoreElements()){  
    71.             String key=(String)keys.nextElement();  
    72.             String value=(String)this.attributes.get(key);  
    73.             xml.append(" "+ key+ "=" + value + " ");  
    74.         }  
    75.         if (((this.getValue()==null)||(this.getValue().length()==0))&&(this.childNodes.size()==0)){  
    76.            xml.append(" /> ");  
    77.         }else{  
    78.            xml.append(" >");  
    79.            if ((this.getValue()!=null)&&(this.getValue().length()>0)){  
    80.                    xml.append(this.getValue());  
    81.            }          
    82.            for(int i=0;i<this.childNodes.size();i++)  
    83.              xml.append(((XMLNode)this.childNodes.elementAt(i)).toString());  
    84.            xml.append("</"+this.getName()+"> ");           
    85.         }  
    86.         return xml.toString();  
    87.     }  
    88.   
    89.     public Hashtable getAttributes() {  
    90.         return attributes;  
    91.     }  
    92.   
    93.     public Vector getChildNodes() {  
    94.         return childNodes;  
    95.     }  
    96. }  

     类就两个。很简单

    看看Demo吧。

     

    Java代码 复制代码
    1. String _res = Tools.toUTFString(Tools.read(this.getClass().  
    2.                              getResourceAsStream(_url)));  
    3.                      XMLParser tXMLParser = new XMLParser(_res);  
    4.                      XMLNode tXMLNode= tXMLParser.parse();  
    5.                      PlayerItem tMp4 = new PlayerItem(tXMLNode.getAttribute("name"), tXMLNode.getAttribute("encode"));  
    6.                      for(int i=0; i<tXMLNode.getChildNodes().size(); i++){  
    7.                          XMLNode tNode = (XMLNode) tXMLNode.getChildNodes().elementAt(i);  
    8.                            
    9.                          tMp4.addQueue(tNode.getValue());  
    10.                      }  
    11.   
    12.                      PlayerPanel tMP4Panel = new PlayerPanel(tMp4, getPlayerList());  
    13.                      tMP4Panel.show();  
    14.   
    15. 上面的例子是我从自己实现了一个J2ME流媒体播放器抽出来的,大家凑合着看。  

     

    解释就这么简单。欢迎大家讨论。有啥需要讨论的东西,留言沟通

    发表于 @ 2008年05月28日 12:04:28|评论(loading...)|编辑

    新一篇: 我的第一个Android 多媒体的Demo | 旧一篇: 探讨J2ME 流媒体的实现

    评论:没有评论。

    发表评论  


    登录
    Csdn Blog version 3.1a
    Copyright © 无花