android_好友名單XML解析構造

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   ar   color   os   使用   

首先是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解析構造

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.