0

Hi I am trying to Parse a local xml file (which has database table information) and ultimately store the information in SQLite DB. It is stored in the raw folder which I created under the res.

But I don't know how to open it and Parse using SAX parser. Went through different tutorials but did't work.

If Someone can guide me its great. Thank you

Wesley Wiser
  • 9,491
  • 4
  • 50
  • 69
JibW
  • 4,538
  • 17
  • 66
  • 101
  • See http://stackoverflow.com/questions/6079637/xmlpullparser-how-to-attain-res-raw-xml-xmlfilename. – Femi Jul 11 '11 at 16:49
  • I need to Use SAX parser. I think its efficient. Do You have any idea how to open the local xml file and parse it with the SAX parser. And after that I need to store the information in the SQLite DB. Will it be possible OR any other easy and an efficient way is there??? – JibW Jul 11 '11 at 17:06

2 Answers2

0

You can get an InputStream using openRawResource (getResources().openRawResource(R.id.{resourcename})). You can then pass that InputStream to your SAX parser like you would if it was a regular file.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • 1
    See http://stackoverflow.com/questions/4991839/android-parsing-large-files-using-sax-parser for more detail: that actually looks like what you want (it also describes some size constraints you may have to deal with). – Femi Jul 11 '11 at 17:24
  • Ya... that worked. openRawResource (getResources().openRawResource(R.id.{resourcename})). Is there a easy way to convert that inputstream to String????? – JibW Jul 13 '11 at 17:19
  • Well, http://stackoverflow.com/questions/309424/in-java-how-do-a-read-convert-an-inputstream-in-to-a-string has some options you may want to consider. – Femi Jul 13 '11 at 17:39
0

I created an XML parser on Android for XML data that was in a byte array as follows:

Create a class that extends DefaultHandler.

Create a method in this class similar to the following:

public void ParseXMLByteArray(byte[] xmlData, int len)
{
    SAXParserFactory spf = SAXParserFactory.newInstance();
    try
    {
        // Create a SAX parser
        SAXParser mySP = spf.newSAXParser();
        // Get an XML reader from the parser
        XMLReader myXR = mySP.getXMLReader();
        // Tell the reader this class will supply content callbacks
        myXR.setContentHandler(this);
        // Kick off the parse operation
        // You will get ContentHandler callbacks via DefaultHandler overrides 
        myXR.parse(new InputSource(new ByteArrayInputStream(xmlData, 0, len)));
    }
    catch ( Exception excp )
    {
        // Handle exceptions
    }
}

You would of course want to make the input source your local file. Your DefaultHandler overrides would process the elements and add them to your database.

Jim Rhodes
  • 5,021
  • 4
  • 25
  • 38