DOM方式xml解析

package com.example.alexanderlee.newspaper.parser;

import android.os.AsyncTask;
import android.util.Log;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import static com.example.alexanderlee.newspaper.view.MainActivity.TAG;

/**
 * Created by alexanderlee on 2018/4/16.
 */

public class XmlParser {

    public static final String xmlUrl = "http://oxm0wpojp.bkt.clouddn.com/newspaper.xml";

    public ArrayList<HashMap<String, String>> newsList = new ArrayList<HashMap<String, String>>();

    public static final String KEY_TITLE = "title";
    public static final String KEY_CONTENT = "content";
    public static final String KEY_IMAGEURL = "imageUrl";

    public XmlParser(ArrayList<HashMap<String, String>> newsList) {
        this.newsList = newsList;
        new XmlPaserAsyncTask().execute();
    }

    class XmlPaserAsyncTask extends AsyncTask {

        @Override
        protected Object doInBackground(Object[] objects) {
            try {
                URL url = new URL(xmlUrl);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                Log.d(TAG, "hello");
                conn.setConnectTimeout(5000);
                Log.d(TAG, "conn.getResponseCode()" + conn.getResponseCode());
                if (conn.getResponseCode() == 200) {
                    Log.d(TAG, String.valueOf(conn.getResponseCode()));
                    InputStream is = conn.getInputStream();
                    Log.d(TAG, "is=" + is);
                    xmlPaser(is);
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {

            }
            return null;
        }
    }

    private void xmlPaser(InputStream is) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document document = db.parse(is);
            Element root = document.getDocumentElement();
            NodeList nodeList = root.getElementsByTagName("news");
            for (int i = 0; i < nodeList.getLength(); i++) {

                Element newsElement = (Element) nodeList.item(i);
                String id = newsElement.getAttribute("id");
                HashMap<String, String> map = new HashMap<>();

                Element titleElement = (Element) newsElement.getElementsByTagName(KEY_TITLE).item(0);
                String title = titleElement.getTextContent();
                map.put(KEY_TITLE, title);

                Element contentElement = (Element) newsElement.getElementsByTagName(KEY_CONTENT).item(0);
                String content = contentElement.getTextContent();
                map.put(KEY_CONTENT, content);

                Element imageUrlElement = (Element) newsElement.getElementsByTagName(KEY_IMAGEURL).item(0);
                String imageUrl = imageUrlElement.getTextContent();
                map.put(KEY_IMAGEURL, imageUrl);

                newsList.add(map);
            }

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

阅读更多
上一篇Android版本号
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭