这是我的大问题。根据我搜索过的选项,一个是解析我需要的数据然后存储在 sqlite 中,另一个是将整个 xml 文件作为文本存储在 sqlite 中。我实际上不知道从哪里开始,但我认为第二个选项对我来说更可取。有人能指出我该怎么做吗?或者如果有更多关于我的问题的建议。
任何回复将不胜感激..谢谢!
这是我的大问题。根据我搜索过的选项,一个是解析我需要的数据然后存储在 sqlite 中,另一个是将整个 xml 文件作为文本存储在 sqlite 中。我实际上不知道从哪里开始,但我认为第二个选项对我来说更可取。有人能指出我该怎么做吗?或者如果有更多关于我的问题的建议。
任何回复将不胜感激..谢谢!
对不起,我的英语不是很好。
我会选择第二个选项。您可以使用 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;
}
如果您将数据解析一次并存储在数据库中,那就更好了。u 只能查询需要的数据并显示。
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;
}
}
}