如何在SD卡上读取xml数据?

时间:2017-04-22 13:33:52

标签: java android arrays xml xmlreader

我无法弄清楚读取部分的算法。 (我知道英语很少) 我想算法中只有一个错误。   我也在寻找电话簿备份示例应用程序   如果你帮助我,我会很高兴的

    ArrayList<String> ad;
    ArrayList<String> phone_no;


    ad = new ArrayList<String>();
    phone_no = new ArrayList<String>();

 try {       File file = new File("/sdcard/TELEFON_YEDEK/yedek.xml");
        InputStream is = new FileInputStream(file.getPath());
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new InputSource(is));
        doc.getDocumentElement().normalize();

        NodeList nodeList = doc.getElementsByTagName("TelefonRehberi");
        NodeList n = doc.getElementsByTagName("kisi");
        for (int i = 0; i < n.getLength(); i++) {

            Node node = n.item(i);

            Element fstElmnt = (Element) node;

            ad.add(fstElmnt.getAttribute("adSoyad"));
            count = Integer.parseInt(ad.get(i));

        }
        for (int j = 0; j < count; j++) {
            Node node = n.item(j);

            Element fstElmnt = (Element) node;

            phone_no.add(fstElmnt.getAttribute("telefon"));
            Log.d("adsoyadlar",fstElmnt.getAttribute("telefon"));
        }
    } catch (Exception e) {
        Toast.makeText(MainActivity.this,"okunamadı",Toast.LENGTH_SHORT).show();
    }

xml file

1 个答案:

答案 0 :(得分:0)

使用以下代码从xml读取:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;

import android.os.Environment;
import android.util.Log;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import android.content.Context;

        try {
        FileInputStream fis;
        ArrayList<String> ad;
        ArrayList<String> phone_no;

        ad = new ArrayList<String>();
        phone_no = new ArrayList<String>();

        // temp holder for current text value while parsing
        String curText = "";
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();

        XmlPullParser xpp = factory.newPullParser();
        // Open up InputStream and Reader of our file.
        fis =  new FileInputStream(new File("/sdcard/TELEFON_YEDEK/yedek.xml"));
        BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
        // point the parser to our file.

        xpp.setInput(reader);
        // get initial eventType

        int eventType = xpp.getEventType();
        // Loop through pull events until we reach END_DOCUMENT

        while (eventType != XmlPullParser.END_DOCUMENT) {

            // Get the current tag

            String tagname = xpp.getName();
            // React to different event types appropriately

            switch (eventType) {

            case XmlPullParser.TEXT:
                //grab the current text so we can use it in END_TAG event
                curText = xpp.getText();
                break;
            case XmlPullParser.END_TAG:
                if (tagname.equalsIgnoreCase("adSoyad")) {
                    // if </adSoyad> add on adSoyad
                    ad.add(curText);
                } else if (tagname.equals("telefon")) {
                    // if </telefon> use setLink() on telefon
                    phone_no.add(curText);
                }
                break;
            default:
                break;
            }
            //move on to next iteration
            eventType = xpp.next();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        fis.close();
    }catch(Exception e){
        Log.i("Problem closing", "Closing fis");
    }