繁体   English   中英

如何读取SD卡上的XML数据?

[英]How to read xml data on sd card?

我无法在阅读部分弄清楚算法。 (我不太懂英语)我猜算法中只有一个错误。 我也在寻找电话簿备份示例应用程序。
如果你能帮助我,我会很高兴

    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文件

使用以下代码从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");
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM