现在的位置: 首页 > 综合 > 正文

DOM读取sdcard中的文件

2019年07月20日 ⁄ 综合 ⁄ 共 3363字 ⁄ 字号 评论关闭

 

 

 

在main.xml中:

<?xml version="1.0" encoding="utf-8"?>

<TableLayout

  xmlns:android="http://schemas.android.com/apk/res/android"

  android:orientation="vertical"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent">

  <TableRow

      android:gravity="center_horizontal"

      android:layout_margin="8dp">

     <TextView

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:text="姓名:" />

     <TextView

       android:id="@+id/name"

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"/>

  </TableRow>

  <TableRow

      android:gravity="center_horizontal">

     <TextView

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:text="邮箱:" />

     <TextView

       android:id="@+id/email"

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"/>

  </TableRow>

  <TableRow

      android:layout_marginLeft="30dp">

     <Button

       android:id="@+id/but"

       android:layout_width="60dp"

       android:layout_height="wrap_content"

       android:text="读取" />

  </TableRow>

</TableLayout>

 

 

 

在MyDOMDemo.java程序中

 

package com.li.dom;

 

import java.io.File;

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 android.app.Activity;

import android.os.Bundle;

import android.os.Environment;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

 

public class MyDOMDemo extends Activity {

  private TextView name = null;

  private TextView email = null;

  private Button but = null;

 

  @Override

  public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);

     super.setContentView(R.layout.main);

     this.name = (TextView) super.findViewById(R.id.name);

     this.email = (TextView) super.findViewById(R.id.email);

     this.but = (Button) super.findViewById(R.id.but);

     this.but.setOnClickListener(new OnClickListenerImpl());

  }

 

  private class OnClickListenerImpl implements OnClickListener {

 

     public void onClick(View v) {

       if (!Environment.getExternalStorageState().equals(

            Environment.MEDIA_MOUNTED)) { // 不存在不操作

         return; // 返回到程序的被调用处

       }

       File file = new File(Environment.getExternalStorageDirectory()

            + File.separator + "liyewen" + File.separator

            + "test.xml"); // 要输出文件的路径

       if (!file.exists()) { // 文件不存在

         return;

       }

       DocumentBuilderFactory factory = DocumentBuilderFactory

            .newInstance();

       DocumentBuilder builder = null;

       try {

         builder = factory.newDocumentBuilder();

       } catch (ParserConfigurationException e) {

         e.printStackTrace();

       }

       Document doc = null;

       try {

         doc = builder.parse(file); // 通过文件转化文档

       } catch (SAXException e1) {

         e1.printStackTrace();

       } catch (IOException e1) {

         e1.printStackTrace();

       }

       NodeList nl = doc.getElementsByTagName("linkman");

       for (int x = 0; x < nl.getLength(); x++) {

         Element e = (Element) nl.item(x); // 取得元素

         MyDOMDemo.this.name.setText(e.getElementsByTagName("name")

              .item(0).getFirstChild().getNodeValue());

         MyDOMDemo.this.email.setText(e.getElementsByTagName("email")

              .item(0).getFirstChild().getNodeValue());

       }

     }

 

  }

}

 

【上篇】
【下篇】

抱歉!评论已关闭.