<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ? 大家好今天我今天給大家講解一下android中xml的創建以及一些解析xml的常用方法。 首先是創建,我們用XmlSerializer這個類來創建一個xml文件,其次是解析xml文件,常用的有dom,sax,XmlPullParser等方法,由于sax代碼有點復雜,本節只講解一下dom與XmlPullParser解析,sax我將會在下一節單獨講解,至于幾種解析xml的優缺點我就不再講述了。 為了方便理解,我做了一個簡單的Demo。首先首界面有三個按鈕,點擊第一個按鈕會在sdcard目錄下創建一個books.xml文件,另外兩個按鈕分別是調用dom與XmlPullParser方法解析xml文件,并將結果顯示在一個TextView里。大家可以按照我的步驟一步步來: 第一步:新建一個Android工程,命名為XmlDemo. 第二步:修改main.xml布局文件,代碼如下: ~~~ <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/btn1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="創建XML文件" /> <Button android:id="@+id/btn2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="DOM解析XML" /> <Button android:id="@+id/btn3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="XmlPullParse解析XML" /> <TextView android:id="@+id/result" android:layout_width="fill_parent" android:layout_height="wrap_content" /></LinearLayout> ~~~ 第三步:修改主核心程序XmlDemo.java,代碼如下: ~~~ package com.tutor.xml;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NodeList;import org.xml.sax.SAXException;import org.xmlpull.v1.XmlPullParser;import org.xmlpull.v1.XmlPullParserException;import org.xmlpull.v1.XmlPullParserFactory;import org.xmlpull.v1.XmlSerializer;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.util.Xml;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;public class XmlDemo extends Activity implements OnClickListener { private static final String BOOKS_PATH = "/sdcard/books.xml"; private Button mButton1,mButton2,mButton3; private TextView mTextView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setupViews(); } //初始化工作 private void setupViews(){ mTextView = (TextView)findViewById(R.id.result); mButton1 = (Button)findViewById(R.id.btn1); mButton2 = (Button)findViewById(R.id.btn2); mButton3 = (Button)findViewById(R.id.btn3); mButton1.setOnClickListener(this); mButton2.setOnClickListener(this); mButton3.setOnClickListener(this); } //創建xml文件 private void createXmlFile(){ File linceseFile = new File(BOOKS_PATH); try{ linceseFile.createNewFile(); }catch (IOException e) { Log.e("IOException", "exception in createNewFile() method"); } FileOutputStream fileos = null; try{ fileos = new FileOutputStream(linceseFile); }catch (FileNotFoundException e) { Log.e("FileNotFoundException", "can't create FileOutputStream"); } XmlSerializer serializer = Xml.newSerializer(); try { serializer.setOutput(fileos,"UTF-8"); serializer.startDocument(null, true); serializer.startTag(null, "books"); for(int i = 0; i < 3; i ++){ serializer.startTag(null, "book"); serializer.startTag(null, "bookname"); serializer.text("Android教程" + i); serializer.endTag(null, "bookname"); serializer.startTag(null, "bookauthor"); serializer.text("Frankie" + i); serializer.endTag(null, "bookauthor"); serializer.endTag(null, "book"); } serializer.endTag(null, "books"); serializer.endDocument(); serializer.flush(); fileos.close(); } catch (Exception e) { Log.e("Exception","error occurred while creating xml file"); } Toast.makeText(getApplicationContext(), "創建xml文件成功!", Toast.LENGTH_SHORT).show(); } //dom解析xml文件 private void domParseXML(){ File file = new File(BOOKS_PATH); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = null; try { db = dbf.newDocumentBuilder(); } catch (ParserConfigurationException e) { e.printStackTrace(); } Document doc = null; try { doc = db.parse(file); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Element root = doc.getDocumentElement(); NodeList books = root.getElementsByTagName("book"); String res = "本結果是通過dom解析:" + "/n"; for(int i = 0; i < books.getLength();i++){ Element book = (Element)books.item(i); Element bookname = (Element)book.getElementsByTagName("bookname").item(0); Element bookauthor = (Element)book.getElementsByTagName("bookauthor").item(0); res += "書名: " + bookname.getFirstChild().getNodeValue() + " " + "作者: " + bookauthor.getFirstChild().getNodeValue() + "/n"; } mTextView.setText(res); } //xmlPullParser解析xml文件 private void xmlPullParseXML(){ String res = "本結果是通過XmlPullParse解析:" + "/n"; try { XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlPullParser xmlPullParser = factory.newPullParser(); xmlPullParser.setInput(Thread.currentThread().getContextClassLoader() .getResourceAsStream(BOOKS_PATH), "UTF-8"); int eventType = xmlPullParser.getEventType(); try{ while (eventType != XmlPullParser.END_DOCUMENT) { String nodeName = xmlPullParser.getName(); switch (eventType) { case XmlPullParser.START_TAG: if("bookname".equals(nodeName)){ res += "書名: " + xmlPullParser.nextText() + " "; }else if("bookauthor".equals(nodeName)){ res += "作者: " + xmlPullParser.nextText() + "/n"; } break; default: break; } eventType = xmlPullParser.next(); } } catch (IOException e) { e.printStackTrace(); } } catch (XmlPullParserException e) { e.printStackTrace(); } mTextView.setText(res); } //按鈕事件響應 public void onClick(View v) { if(v == mButton1){ createXmlFile(); }else if(v == mButton2){ domParseXML(); }else if(v == mButton3){ xmlPullParseXML(); } } } ~~~ 第四步:由于我們在Sd卡上新建了文件,需要增加權限,如下代碼(第16行): ~~~ <?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tutor.xml" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".XmlDemo" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="7" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> </manifest> ~~~ 第五步:運行上述工程,查看效果: 啟動首界面: ![](https://box.kancloud.cn/2016-08-10_57aaf0ea84dd3.gif) 點擊創建XML文件按鈕,生成books.xml文件 ![](https://box.kancloud.cn/2016-08-10_57aaf0ea9cf2c.gif) books.xml內容如下: ~~~ <?xml version='1.0' encoding='UTF-8' standalone='yes' ?><books> <book> <bookname>Android教程0</bookname> <bookauthor>Frankie0</bookauthor> </book> <book> <bookname>Android教程1</bookname> <bookauthor>Frankie1</bookauthor> </book> <book> <bookname>Android教程2</bookname> <bookauthor>Frankie2</bookauthor> </book></books> ~~~ 點擊DOM解析XML按鈕: ![](https://box.kancloud.cn/2016-08-10_57aaf0ead02a6.gif) 點擊XmlPullParse解析XML按鈕: ![](https://box.kancloud.cn/2016-08-10_57aaf0ead02a6.gif) Ok~今天就先講到這里。thx~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看