zoukankan      html  css  js  c++  java
  • Android之SAX解析Xml文件实现

    public class JokeBean {
    private String id;
    private String title;
    private StringBuilder text=new StringBuilder();
    public JokeBean(){
    }

    public String getId() {
    return id;
    }

    public void setId(String id) {
    this.id = id;
    }

    public String getTitle() {
    return title;
    }

    public void setTitle(String title) {
    this.title = title;
    }

    public String getText() {
    return text.toString();
    }

    public void setText(String text) {
    this.text.append(text);
    }
    }

    然后是解析类,继承DefaultHandler类:

    public class SAXHandler extends DefaultHandler{
    private static final String TAG = "SAXHandler";
    private List<JokeBean> data;
    private JokeBean joke;
    private String preTAG;

    private static final String JOKE="joke";
    private static final String JOKE_ID="id";
    private static final String JOKE_TITLE="title";
    private static final String JOKE_TEXT="text";

    public SAXHandler(List<JokeBean> data){
    super();
    this.data = data;
    }

    @Override
    public void startDocument() throws SAXException {
    if(data==null)
    data=new ArrayList<JokeBean>();
    Log.d(TAG, "startDocument ------- :");
    super.startDocument();
    }
    @Override
    public void endDocument() throws SAXException {
    Log.d(TAG, "endDocument ------- :");
    super.endDocument();
    }

    @Override
    public void startElement(String uri, String localName, String qName,
    Attributes attributes) throws SAXException {
    Log.d(TAG, "endDocument ------- :localName "+localName);

    preTAG = localName;
    if(JOKE.equals(localName)){
    joke=new JokeBean();
    }

    super.startElement(uri, localName, qName, attributes);
    }

    @Override
    public void endElement(String uri, String localName, String qName)
    throws SAXException {
    preTAG = "";
    if(JOKE.equals(localName)){
    data.add(joke);
    Log.d(TAG, "endElement ------- :一个解析对象完成 。。。。 ");
    }
    super.endElement(uri, localName, qName);
    }

    @Override
    public void characters(char[] ch, int start, int length)
    throws SAXException {
    String info = new String(ch,start,length);
    Log.d(TAG, "characters ------- : info" +info);
    if(preTAG.equals(JOKE_ID)){
    joke.setId(info);
    }else if(preTAG.equals(JOKE_TITLE)){
    joke.setTitle(info);
    }else if(preTAG.equals(JOKE_TEXT)){
    joke.setText(info);
    }
    super.characters(ch, start, length);
    }

    public List<JokeBean> getdata(){
    return data;
    }
    }

    封装解析xml的工具类,方便客户端调用:

    public class XMLUtil {

    public static List<JokeBean> parserXML(Context context,int rawId){
    List<JokeBean> data = new ArrayList<JokeBean>();
    SAXParserFactory factory=SAXParserFactory.newInstance();
    XMLReader reader;
    try {
    reader = factory.newSAXParser().getXMLReader();
    reader.setContentHandler(new SAXHandler(data));
    InputSource is = new InputSource(context.getResources().openRawResource(rawId));
    is.setEncoding("utf-8");
    reader.parse(is);
    } catch (Exception e) {
    e.printStackTrace();
    }
    return data;
    }
    }

    客户端主类代码:

    public class MainActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    Button btnSAX, btnOutput;
    EditText memo;
    private List<JokeBean> data;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    memo = (EditText)findViewById(R.id.EditText01);
    findViewById(R.id.btnOutput).setOnClickListener(this);
    }

    public void onClick(View v) {
    if(v.getId()==R.id.btnOutput){
    mHandler.post(new Runnable() {
    public void run() {
    data=XMLUtil.parserXML(MainActivity.this,R.raw.joke2);
    memo.setText(data.get(0).getText());
    }
    });
    }
    }
    private Handler mHandler = new Handler();
    }

    xml的格式:

    <?xml version="1.0" encoding="UTF-8" ?> 
    <jokes>
    <joke>
    <id>1</id>
    <title>笑话一</title>
    <text><![CDATA[我爱人民]]></text>
    </joke>
    </jokes>

    参考:

    http://blog.csdn.net/boyupeng/article/details/6304827




  • 相关阅读:
    线程安全与可重入编写方法
    新手MySQL工程师必备命令速查手册
    分布式之数据库和缓存双写一致性方案解析
    在java代码中用xslt处理xml文件
    Java并发编程之并发代码设计
    Java多线程和并发基础
    Java多线程面试大全
    springboot获取URL请求参数的多种方式
    JAVA文件转换为Base64
    Silver Cow Party
  • 原文地址:https://www.cnblogs.com/slider/p/2348772.html
Copyright © 2011-2022 走看看