首页 > 代码库 > android_好友列表XML解析构造

android_好友列表XML解析构造

首先是FriendBean

package com.example.other;public class FriendBean{    private String FName = null;    private String FImg = null;    private int iID = -1;    public String getFName()    {        return FName;    }    public void setFName(String fName)    {        FName = fName;    }    public String getFImg()    {        return FImg;    }    public void setFImg(String fImg)    {        FImg = fImg;    }        public int getID()    {        return iID;    }    public void setID(int iId)    {        this.iID = iId;    }    }


然后新建类FriendXMLContentHandler,使用sax解析xml

package com.example.other;import java.util.ArrayList;import java.util.List;import org.xml.sax.Attributes;import org.xml.sax.SAXException;import org.xml.sax.helpers.DefaultHandler;/* *  * 解析如下xml * <friend id="1"> * <fname></fname> * <fimg></fimg> * </friend> * <friend>。。。。 *  *  * 使用: * public static List<friend> readXML(InputStream inStream)  * {    *         try {             *            //创建解析器             *            SAXParserFactory spf = SAXParserFactory.newInstance();            *            SAXParser saxParser = spf.newSAXParser();              *            //设置解析器的相关特性,true表示开启命名空间特性             *            saxParser.setProperty("http://xml.org/sax/features/namespaces",true);             *            XMLContentHandler handler = new XMLContentHandler();             *            saxParser.parse(inStream, handler);             *            inStream.close();             *            return handler.getAllFriends();    *            }  *            catch (Exception e)  *            {             *                e.printStackTrace();    *            }  *      return null; *} *  * */public class FriendXMLContentHandler extends DefaultHandler{    private List<FriendBean> m_lFriends = null;    private FriendBean m_currentFriend = null; // 记录当前对象    private String m_tagName = null; // 当前解析的元素标签    public List<FriendBean> getAllFriends()    {        return m_lFriends;    }    // 接到文档开始    @Override    public void startDocument() throws SAXException    {        // TODO Auto-generated method stub        m_lFriends = new ArrayList<FriendBean>();        super.startDocument();    }    // 开始解析    @Override    public void startElement(String uri, String localName, String qName,            Attributes attributes) throws SAXException    {        // TODO Auto-generated method stub        if ( localName.equals("friend") )        {            m_currentFriend = new FriendBean();            m_currentFriend.setID(Integer.parseInt(attributes.getValue("id")));        }        this.m_tagName = localName;    }    // 进入friend内    @Override    public void characters(char[] ch, int start, int length)            throws SAXException    {        // TODO Auto-generated method stub        if ( m_tagName != null )        {            String data = new String(ch, start, length);            if ( m_tagName.equals("fname") )            {                this.m_currentFriend.setFName(data);            } else if ( m_tagName.equals("fimg") )            {                this.m_currentFriend.setFImg(data);            }        }    }    //    @Override    public void endElement(String uri, String localName, String qName)            throws SAXException    {        // TODO Auto-generated method stub        if ( localName.equals("friend") )        {            m_lFriends.add(m_currentFriend);            m_currentFriend = null;        }        this.m_tagName = null;    }}

 

android_好友列表XML解析构造