在Android中的SQLite数据库中插入远程(Internet)xml文件

时间:2012-05-21 05:15:30

标签: android database sqlite xml-parsing

这是我的大问题。基于我用google搜索的选项,一个是解析我需要的数据然后存储在sqlite中,另一个是将整个xml文件存储为sqlite中的文本。我实际上不知道从哪里开始,但我认为第二种选择对我来说更可取..有人能指出我将如何做到这一点?或者如果有更多关于我的问题的建议在这里..

任何回复都会非常感谢。谢谢!

2 个答案:

答案 0 :(得分:0)

抱歉,但我的英语不是很好。

我会选择第二种选择。您可以使用XmlPullParser和XmlPullParserFactory读取文件XML:

    XmlPullParser xmlPullParser;  
    String output; 
    XmlPullParserFactory fabrica = null;

    File archivoXml = new File(Environment.getExternalStorageDirectory() + servicioXML); //servicioXML is a xml path 
    FileInputStream fisArchivo = null;
    XmlPullParser analizador = null;
    int tipoEvento = 0; 

    try {
        fisArchivo = new FileInputStream(archivoXml);
        fabrica = XmlPullParserFactory.newInstance();
        fabrica.setNamespaceAware(true);
        analizador = fabrica.newPullParser();
        analizador.setInput(new InputStreamReader(fisArchivo));
        fabrica = XmlPullParserFactory.newInstance();  
        xmlPullParser = fabrica.newPullParser();  
        xmlPullParser.setInput(fisArchivo, "UTF-8"); 
        tipoEvento = xmlPullParser.getEventType(); 

    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();

    } catch (XmlPullParserException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    while (tipoEvento != XmlPullParser.END_DOCUMENT) {  
        analizarEvento(tipoEvento);  
        try {  
            tipoEvento = xmlPullParser.next();  
        } catch (XmlPullParserException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
    } 
}  


private void analizarEvento(int evento){  
    switch (evento) {  
    case XmlPullParser.START_DOCUMENT:  
        break;  

    case XmlPullParser.END_DOCUMENT:    
        break;  

    case XmlPullParser.START_TAG:   
        break;  

    case XmlPullParser.END_TAG:  
        break;  

    case XmlPullParser.TEXT:  
        output = xmlPullParser.getText();  
        AdapterBBDD.setDBField(output); //add the xml data to the database
        break;  
    }  

更多信息:XmlPullParser in Android

答案 1 :(得分:0)

如果您将数据解析一次并存储在数据库中,则更好。你只能查询所需的数据并显示。

 SAXParserFactory spf = SAXParserFactory.newInstance();
                    SAXParser sp = spf.newSAXParser();
                    XMLReader xr = sp.getXMLReader();
                    /** Create handler to handle XML Tags ( extends DefaultHandler ) */
                    XMLHandler dbXMLHandler = new XMLHandler();
                    xr.setContentHandler(dbXMLHandler);
                    InputStream is = new ByteArrayInputStream(
                            xmlData.getBytes("UTF-8"));
                    xr.parse(new InputSource(is));

    phoneList = XMLHandler.phoneList;
                for(int id = 0; id < phoneList.getID().size();id++)
                {
                    database.open();
                    database.insertupdateintoPhonebook(phoneList.getUserName().get(id),
                            phoneList.getUserName().get(id)
                                    .get(id), phoneList.getDOB().get(id)

                    database.close();
}

和DBXMLHAndler.java如下

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;



public class XMLHandler extends DefaultHandler{
Boolean currentElement = false;
String currentValue = null;
public static Phonelist phoneList = null;

public static Phonelist getPhoneList() {
    return phoneList;
}

public static void setPhonelist(Phonelist phoneList) {
    DBXMLHandler.phoneList = phoneList;
}
@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {

    currentElement = true;

    if (localName.equals("NewDataSet"))
    {
        phoneList = new Phonelist();
    }

}

/** Called when tag closing ( ex:- <name>AndroidPeople</name> 
 * -- </name> )*/
@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {

    currentElement = false;

    /** set value */ 
    if (localName.equalsIgnoreCase("Id"))
        phoneList.setID(currentValue);
    else if (localName.equalsIgnoreCase("Username"))
        phoneList.setUserName(currentValue);    
    else if (localName.equalsIgnoreCase("Dob"))
        phoneList.setDOB(currentValue);

}

/** Called to get tag characters ( ex:- <name>AndroidPeople</name> 
 * -- to get AndroidPeople Character ) */
@Override
public void characters(char[] ch, int start, int length)
        throws SAXException {

    if (currentElement) {
        currentValue = new String(ch, start, length);
        currentElement = false;
    }

}

}