zoukankan      html  css  js  c++  java
  • 使用SAX方式解析XML文件

    package com.pingyijinren.test;
    
    import android.util.Log;
    
    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;
    
    /**
     * Created by Administrator on 2016/5/19 0019.
     */
    public class ContentHandler extends DefaultHandler {
        private String nodeName;
        private StringBuilder id;
        private StringBuilder name;
        private StringBuilder version;
    
        @Override
        public void startDocument() throws SAXException{
            id=new StringBuilder();
            name=new StringBuilder();
            version=new StringBuilder();
        }
    
        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException{
            nodeName=localName;
        }
    
        @Override
        public void characters(char[] ch,int start,int length) throws SAXException{
            if(nodeName.equals("id")){
                id.append(ch,start,length);
            }
            else if(nodeName.equals("name")){
                name.append(ch,start,length);
            }
            else if(nodeName.equals("version")){
                version.append(ch,start,length);
            }
        }
    
        @Override
        public void endElement(String uri,String localName,String qName) throws SAXException{
            if(localName.equals("app")){
                Log.d("MainActivity","id is "+id.toString().trim());
                Log.d("MainActivity","name is "+name.toString().trim());
                Log.d("MainActivity","version is "+version.toString().trim());
    
                id.setLength(0);
                name.setLength(0);
                version.setLength(0);
            }
        }
    
        @Override
        public void endDocument() throws SAXException{}
    }
    package com.pingyijinren.test;
    
    import android.content.Intent;
    import android.os.Handler;
    import android.os.Message;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.util.EntityUtils;
    import org.xml.sax.InputSource;
    import org.xml.sax.XMLReader;
    import org.xmlpull.v1.XmlPullParser;
    import org.xmlpull.v1.XmlPullParserFactory;
    
    import java.io.BufferedReader;
    import java.io.EOFException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.StringReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    import javax.xml.parsers.SAXParserFactory;
    
    public class MainActivity extends AppCompatActivity{
        private Button button;
        private TextView textView;
        private static final int SHOW_RESPONSE=0;
        private Handler handler=new Handler(){
            public void handleMessage(Message message){
                switch(message.what){
                    case SHOW_RESPONSE:
                        String response=(String)message.obj;
                        textView.setText(response);
                }
            }
        };
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            button=(Button)findViewById(R.id.button);
            textView=(TextView)findViewById(R.id.textView);
    
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    sendRequestWithHttpURLConnection();
                }
            });
        }
    
        private void sendRequestWithHttpURLConnection(){
            new Thread(new Runnable(){
                @Override
                public void run(){
                    try{
                        HttpClient httpClient = new DefaultHttpClient();
                        HttpGet httpGet=new HttpGet("http://172.29.209.1/get_data.xml");
                        HttpResponse httpResponse=httpClient.execute(httpGet);
                        if(httpResponse.getStatusLine().getStatusCode()==200){
                            HttpEntity httpEntity=httpResponse.getEntity();
                            String response= EntityUtils.toString(httpEntity,"utf-8");
    
                            Log.d("MainActivity",response);
    
                            parseXMLWithSAX(response);
                        }
                    }
                    catch(Exception e){
                        e.printStackTrace();
                    }
    
    
                }
            }).start();
        }
    
        private void parseXMLWithSAX(String xmlData){
            try{
                SAXParserFactory factory=SAXParserFactory.newInstance();
                XMLReader xmlReader=factory.newSAXParser().getXMLReader();
                ContentHandler handler=new ContentHandler();
                xmlReader.setContentHandler(handler);
                xmlReader.parse(new InputSource(new StringReader(xmlData)));
            }
            catch(Exception e){
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    ubuntu11.04更改默认JDK
    10个实用的jQuery交互/通信插件和教程
    jquery 使用方法
    在没有安装 ASP.NET MVC3 的服务器上运行 MVC3
    固定 vs. 流动 vs. 弹性:哪种布局更适合你?[SM]
    提升设计品质的8种布局方案[SM]
    Ubuntu 手动安装JDK
    十个简单好用的设计技巧[SM]
    jQuery VSDoc下载地址
    Ubuntu 配置Apache+PHP+MySQL
  • 原文地址:https://www.cnblogs.com/zqxLonely/p/5508245.html
Copyright © 2011-2022 走看看