最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Android之解析XML文件三種方式(DOM,PULL,SAX)

 更新時(shí)間:2017年02月28日 08:43:43   作者:Android流光歲月  
這篇文章主要介紹了詳解Android之解析XML文件三種方式,主要包括DOM,PULL,SAX,有興趣的可以了解一下。

1.xml文件代碼

<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/xml; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page isELIgnored="false" %>
<fqs>
<c:forEach items="${fqs}" var="fq">
      <fq name="${fq.name}">
        <content>${fq.content}</content>
        <time>${fq.time}</time>
      </fq>
  </c:forEach>
</fqs>

2.XML網(wǎng)頁效果圖

3.Android代碼

1.布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/activity_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

  <Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="getXML"
    android:text="獲取XML數(shù)據(jù)" />

  <ListView
    android:id="@+id/lv_main_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

  </ListView>
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/activity_main_pull"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

  <Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="getPULL"
    android:text="獲取PULL數(shù)據(jù)" />

  <ListView
    android:id="@+id/lv_mainpull_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
  </ListView>
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/activity_main_sax"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

  <Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="getSAX"
    android:text="獲取SAX數(shù)據(jù)" />

  <ListView
    android:id="@+id/lv_mainsax_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
  </ListView>
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal">

  <TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/tv_item_listview_name"
    android:layout_weight="1"/>

  <TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/tv_item_listview_content"
    android:layout_weight="1"/>

  <TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/tv_item_listview_time"
    android:layout_weight="1"/>
</LinearLayout>

2.java代碼

DOM解析代碼

public class MainActivity extends AppCompatActivity {

  private ListView lv_main_list;
  private ProgressDialog progressDialog;
  private List<FQ> fqs = new ArrayList<>();
  private MyAdapter myadapter;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv_main_list = (ListView) findViewById(R.id.lv_main_list);

    myadapter = new MyAdapter();
    lv_main_list.setAdapter(myadapter);

    progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("小青正在拼命加載中.....");
  }

  class MyAdapter extends BaseAdapter{

    @Override
    public int getCount() {
      return fqs.size();
    }

    @Override
    public Object getItem(int position) {
      return fqs.get(position);
    }

    @Override
    public long getItemId(int position) {
      return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      if(convertView==null){
        convertView=LayoutInflater.from(MainActivity.this).inflate(R.layout.item_list,null);
        ItemTag itemTag=new ItemTag();
        itemTag.tv_name= (TextView) convertView.findViewById(R.id.tv_item_listview_name);
        itemTag.tv_content= (TextView) convertView.findViewById(R.id.tv_item_listview_content);
        itemTag.tv_tiem= (TextView) convertView.findViewById(R.id.tv_item_listview_time);
        convertView.setTag(itemTag);
      }
      ItemTag itemTag= (ItemTag) convertView.getTag();
      itemTag.tv_name.setText(fqs.get(position).getName());
      itemTag.tv_content.setText(fqs.get(position).getContent());
      itemTag.tv_tiem.setText(fqs.get(position).getTime());

      return convertView;
    }
  }

  public void getXML(View view) {
    new MyTask().execute();
  }

  class MyTask extends AsyncTask {
    //獲取數(shù)據(jù)前
    @Override
    protected void onPreExecute() {
      super.onPreExecute();
      progressDialog.show();
    }

    @Override
    protected Object doInBackground(Object[] params) {
      //獲取網(wǎng)絡(luò)數(shù)據(jù)
      //1.定義獲取網(wǎng)絡(luò)的數(shù)據(jù)的路徑
      String path = "http://192.168.43.149:8080/dataResult.xhtml";
      //2.實(shí)例化URL
      try {
        URL url = new URL(path);
        //3.獲取鏈接對象
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        //4.設(shè)置請求
        httpURLConnection.setRequestMethod("GET");
        //5.設(shè)置請求鏈接超時(shí)的時(shí)間
        httpURLConnection.setConnectTimeout(5000);
        //6.獲取響應(yīng)碼
        int code = httpURLConnection.getResponseCode();
        if (code == 200) {
          //7.獲取返回過來的數(shù)據(jù)(XML)
          InputStream is = httpURLConnection.getInputStream();
          //8.使用DOM解析XML文件
          DocumentBuilderFactory documentBuilderFactory=DocumentBuilderFactory.newInstance();
          DocumentBuilder documentBuilder=documentBuilderFactory.newDocumentBuilder();
          Document document=documentBuilder.parse(is);
          //獲取根標(biāo)簽
          Element root=document.getDocumentElement();
          NodeList nodeList = root.getElementsByTagName("fq");
          for (int i = 0; i < nodeList.getLength(); i++) {
            Element element = (Element) nodeList.item(i);
            //獲取屬性name
            String name = element.getAttribute("name");
            //獲取子標(biāo)簽content,time
            Element elementContent = (Element) element.getElementsByTagName("content").item(0);
            String content = elementContent.getTextContent();
            Element elementTime = (Element) element.getElementsByTagName("time").item(0);
            String time = elementTime.getTextContent();
            FQ fq = new FQ(name, content, time);
            fqs.add(fq);
          }
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
      return fqs;
    }

    //獲取數(shù)據(jù)后更新UI
    @Override
    protected void onPostExecute(Object o) {
      super.onPostExecute(o);
      progressDialog.cancel();
      myadapter.notifyDataSetChanged();
    }
  }
}

PULL解析代碼

public class MainPullActivity extends AppCompatActivity {

  private ListView lv_mainpull_list;
  private ProgressDialog progressDialog;
  private List<FQ> fqs = new ArrayList<>();
  private MyAdapter myadapter;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_pull);
    myadapter = new MyAdapter();
    lv_mainpull_list = (ListView) findViewById(R.id.lv_mainpull_list);
    lv_mainpull_list.setAdapter(myadapter);

    progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("小青正在拼命加載中.....");
  }

  class MyAdapter extends BaseAdapter {

    @Override
    public int getCount() {
      return fqs.size();
    }

    @Override
    public Object getItem(int position) {
      return fqs.get(position);
    }

    @Override
    public long getItemId(int position) {
      return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      if (convertView == null) {
        convertView = LayoutInflater.from(MainPullActivity.this).inflate(R.layout.item_list, null);
        ItemTag itemTag = new ItemTag();
        itemTag.tv_name = (TextView) convertView.findViewById(R.id.tv_item_listview_name);
        itemTag.tv_content = (TextView) convertView.findViewById(R.id.tv_item_listview_content);
        itemTag.tv_tiem = (TextView) convertView.findViewById(R.id.tv_item_listview_time);
        convertView.setTag(itemTag);
      }
      ItemTag itemTag = (ItemTag) convertView.getTag();
      itemTag.tv_name.setText(fqs.get(position).getName());
      itemTag.tv_content.setText(fqs.get(position).getContent());
      itemTag.tv_tiem.setText(fqs.get(position).getTime());

      return convertView;
    }
  }

  public void getPULL(View view) {
    new MyTask().execute();
  }

  class MyTask extends AsyncTask {
    private FQ fq;

    //獲取數(shù)據(jù)前
    @Override
    protected void onPreExecute() {
      super.onPreExecute();
      progressDialog.show();
    }

    @Override
    protected Object doInBackground(Object[] params) {
      //獲取網(wǎng)絡(luò)數(shù)據(jù)
      //1.定義獲取網(wǎng)絡(luò)的數(shù)據(jù)的路徑
      String path = "http://192.168.43.149:8080/dataResult.xhtml";
      //2.實(shí)例化URL
      try {
        URL url = new URL(path);
        //3.獲取鏈接對象
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        //4.設(shè)置請求
        httpURLConnection.setRequestMethod("GET");
        //5.設(shè)置請求鏈接超時(shí)的時(shí)間
        httpURLConnection.setConnectTimeout(5000);
        //6.獲取響應(yīng)碼
        int code = httpURLConnection.getResponseCode();
        if (code == 200) {
          //7.獲取返回過來的數(shù)據(jù)(XML)
          InputStream is = httpURLConnection.getInputStream();
          //8.解析XML
          //使用PULL解析XML文件
          XmlPullParser pullParser= Xml.newPullParser();
          pullParser.setInput(is,"UTF-8");
          int type=pullParser.getEventType();
          while (type!=XmlPullParser.END_DOCUMENT){
            switch (type){
              case XmlPullParser.START_TAG:
                //獲取開始標(biāo)簽名字
                String startTafName=pullParser.getName();
                 if("fq".equals(startTafName)){
                   fq = new FQ();
                   String name=pullParser.getAttributeValue(0);
                   fq.setName(name);
                 }else if ("content".equals(startTafName)){
                   String content=pullParser.nextText();
                   fq.setContent(content);
                 }else if ("time".equals(startTafName)){
                   String time=pullParser.nextText();
                   fq.setTime(time);
                 }
                break;
              case XmlPullParser.END_TAG:
                //獲取接受標(biāo)簽的名字
                String endtagname=pullParser.getName();
                if("fq".equals(endtagname)){
                  fqs.add(fq);
                }
                break;
            }
            type=pullParser.next();
          }
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
      return fqs;
    }

    //獲取數(shù)據(jù)后更新UI
    @Override
    protected void onPostExecute(Object o) {
      super.onPostExecute(o);
      progressDialog.cancel();
      myadapter.notifyDataSetChanged();
    }
  }
}

SAX解析代碼

public class MainSaxActivity extends AppCompatActivity {

  private ListView lv_mainsax_list;
  private ProgressDialog progressDialog;
  private List<FQ> fqs = new ArrayList<>();
  private MyAdapter myadapter;
  private String currentTag = null;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_sax);
    lv_mainsax_list = (ListView) findViewById(R.id.lv_mainsax_list);
    myadapter = new MyAdapter();
    lv_mainsax_list.setAdapter(myadapter);

    progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("小青正在拼命加載中.....");
  }

  class MyAdapter extends BaseAdapter {

    @Override
    public int getCount() {
      return fqs.size();
    }

    @Override
    public Object getItem(int position) {
      return fqs.get(position);
    }

    @Override
    public long getItemId(int position) {
      return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      if (convertView == null) {
        convertView = LayoutInflater.from(MainSaxActivity.this).inflate(R.layout.item_list, null);
        ItemTag itemTag = new ItemTag();
        itemTag.tv_name = (TextView) convertView.findViewById(R.id.tv_item_listview_name);
        itemTag.tv_content = (TextView) convertView.findViewById(R.id.tv_item_listview_content);
        itemTag.tv_tiem = (TextView) convertView.findViewById(R.id.tv_item_listview_time);
        convertView.setTag(itemTag);
      }
      ItemTag itemTag = (ItemTag) convertView.getTag();
      itemTag.tv_name.setText(fqs.get(position).getName());
      itemTag.tv_content.setText(fqs.get(position).getContent());
      itemTag.tv_tiem.setText(fqs.get(position).getTime());

      return convertView;
    }
  }

  public void getSAX(View view) {
    new MyTask().execute();
  }

  class MyTask extends AsyncTask {

    private FQ fq;

    //獲取數(shù)據(jù)前
    @Override
    protected void onPreExecute() {
      super.onPreExecute();
      progressDialog.show();
    }

    @Override
    protected Object doInBackground(Object[] params) {
      //獲取網(wǎng)絡(luò)數(shù)據(jù)
      //1.定義獲取網(wǎng)絡(luò)的數(shù)據(jù)的路徑
      String path = "http://192.168.43.149:8080/dataResult.xhtml";
      //2.實(shí)例化URL
      try {
        URL url = new URL(path);
        //3.獲取鏈接對象
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        //4.設(shè)置請求
        httpURLConnection.setRequestMethod("GET");
        //5.設(shè)置請求鏈接超時(shí)的時(shí)間
        httpURLConnection.setConnectTimeout(5000);
        //6.獲取響應(yīng)碼
        int code = httpURLConnection.getResponseCode();
        if (code == 200) {
          //7.獲取返回過來的數(shù)據(jù)(XML)
          InputStream is = httpURLConnection.getInputStream();
          //8.解析XML
          //使用SAX解析XML文件
          SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
          SAXParser saxParser = saxParserFactory.newSAXParser();
          saxParser.parse(is, new DefaultHandler() {
            @Override
            public void startDocument() throws SAXException {
              super.startDocument();
            }

            @Override
            public void endDocument() throws SAXException {
              super.endDocument();
            }

            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
              super.startElement(uri, localName, qName, attributes);
              currentTag = localName;
              if ("fq".equals(localName)) {
                //實(shí)例化對象
                fq = new FQ();
                String name = attributes.getValue(0);
                fq.setName(name);
              }
            }

            @Override
            public void endElement(String uri, String localName, String qName) throws SAXException {
              super.endElement(uri, localName, qName);
              currentTag=null;
              if ("fq".equals(localName)){
                fqs.add(fq);
              }
            }

            @Override
            public void characters(char[] ch, int start, int length) throws SAXException {
              super.characters(ch, start, length);
              if ("content".equals(currentTag)) {
                String content = new String(ch, start, length);
                fq.setContent(content);
              }else if ("time".equals(currentTag)) {
                String time = new String(ch, start, length);
                fq.setTime(time);
              }
            }
          });
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
      return fqs;
    }

    //獲取數(shù)據(jù)后更新UI
    @Override
    protected void onPostExecute(Object o) {
      super.onPostExecute(o);
      progressDialog.cancel();
      myadapter.notifyDataSetChanged();
    }
  }
}

實(shí)體類

public class FQ {
  private String name;
  private String content;
  private String time;

  public FQ(){}
  public FQ(String name, String time, String content) {
    this.name = name;
    this.time = time;
    this.content = content;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getContent() {
    return content;
  }

  public void setContent(String content) {
    this.content = content;
  }

  public String getTime() {
    return time;
  }

  public void setTime(String time) {
    this.time = time;
  }
}

public class ItemTag {
  public TextView tv_name;
  public TextView tv_content;
  public TextView tv_tiem;
}

配置文件添加聯(lián)網(wǎng)權(quán)限

<!-- 添加聯(lián)網(wǎng)的權(quán)限 -->
<uses-permission android:name="android.permission.INTERNET" />

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android中Image的簡單實(shí)例詳解

    Android中Image的簡單實(shí)例詳解

    這篇文章主要為大家詳細(xì)介紹了Android中Image的簡單實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android進(jìn)階手寫IPC通信框架告別繁瑣AIDL

    Android進(jìn)階手寫IPC通信框架告別繁瑣AIDL

    這篇文章主要為大家介紹了Android進(jìn)階手寫IPC通信框架告別繁瑣AIDL實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • MTK Android平臺(tái)開發(fā)流程

    MTK Android平臺(tái)開發(fā)流程

    這篇文章主要介紹了MTK在Android平臺(tái)開發(fā)的流程,一共分析了44個(gè)步驟,需要的朋友學(xué)習(xí)下吧。
    2017-12-12
  • Android實(shí)現(xiàn)TV端大圖瀏覽效果的全過程

    Android實(shí)現(xiàn)TV端大圖瀏覽效果的全過程

    最近的開發(fā)中遇到了個(gè)需求,需要在tv端加載很長的圖片,發(fā)現(xiàn)網(wǎng)上沒有相關(guān)的資料,所以跟大家分享下,這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)TV端大圖瀏覽效果的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • 關(guān)于Android高德地圖的簡單開發(fā)實(shí)例代碼(DEMO)

    關(guān)于Android高德地圖的簡單開發(fā)實(shí)例代碼(DEMO)

    高德地圖在日常生活中經(jīng)常會(huì)用到,那么基于代碼如何實(shí)現(xiàn)高德地圖呢?下面小編給大家分享一個(gè)demo幫助大家學(xué)習(xí)android高德地圖的簡單開發(fā),需要的朋友參考下
    2016-11-11
  • anndroid使用ViewPager實(shí)現(xiàn)三個(gè)fragment切換

    anndroid使用ViewPager實(shí)現(xiàn)三個(gè)fragment切換

    這篇文章主要介紹了anndroid使用ViewPager實(shí)現(xiàn)三個(gè)fragment切換,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-04-04
  • ViewPager+Fragment實(shí)現(xiàn)側(cè)滑導(dǎo)航欄

    ViewPager+Fragment實(shí)現(xiàn)側(cè)滑導(dǎo)航欄

    這篇文章主要為大家詳細(xì)介紹了ViewPager+Fragment實(shí)現(xiàn)側(cè)滑導(dǎo)航欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • MVVMLight項(xiàng)目之雙向數(shù)據(jù)綁定

    MVVMLight項(xiàng)目之雙向數(shù)據(jù)綁定

    這篇文章主要介紹了MVVMLight項(xiàng)目中雙向數(shù)據(jù)綁定的示例源碼及實(shí)現(xiàn)過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步除夕快樂,新年快樂
    2022-01-01
  • Android Flutter實(shí)現(xiàn)淘寶App的搜索推薦

    Android Flutter實(shí)現(xiàn)淘寶App的搜索推薦

    這篇文章主要為大家詳細(xì)介紹了Android?Flutter如何模擬實(shí)現(xiàn)淘寶App的搜索推薦,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的可以了解一下
    2023-07-07
  • Android CountDownTimer案例總結(jié)

    Android CountDownTimer案例總結(jié)

    這篇文章主要介紹了Android CountDownTimer案例總結(jié),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08

最新評論

太康县| 汾阳市| 锡林浩特市| 浪卡子县| 庆云县| 南岸区| 福清市| 凌源市| 天峻县| 靖西县| 丹江口市| 大同县| 石河子市| 西华县| 洛浦县| 祁阳县| 亚东县| 高邮市| 灵川县| 通海县| 贡山| 太湖县| 麟游县| 玉林市| 曲阳县| 拜泉县| 建湖县| 大安市| 漳州市| 镇江市| 通辽市| 克山县| 体育| 华亭县| 宜良县| 镇巴县| 新邵县| 永和县| 迁安市| 墨竹工卡县| 师宗县|