Android: reading XML from a local resource (for testing)

I am writing an application that will read XML from webservice (possibly through kSOAP2). I am pretty pleased with SAX parsing since I did XML analysis of iPhone applications.

Unfortunately, the webservice is not yet publicly available, so for initial testing, I have some XML files that I need to parse. In this early phase of dev, I just need to read the XML from the files and pass it to the XML parser

Xml.parse(this.testXML, root.getContentHandler()); 

How I read the XML from a file / resource into a string to jump to this method. I want to hack and test the parser, but this simple step holds me back.

thanks

+8
android xml file parsing
source share
3 answers

Create a raw folder under res

Put your XML file there, for example. testXML.xml:

/res/raw/testXML.xml

You should be able to use your XML parser, using this as input:

 Xml.parse(getResources().openRawResource(R.raw.testXML), Xml.Encoding.UTF_8, root.getContentHandler()); 

Try it.

+12
source share

I have found a solution. Use of assets. Here is a simple code example of how I did this. I know that I could use XmlPullParser to just load the XML file from res, but I wanted to use SAX parsing. This allows me to simply move the XML string to the SAX parser for testing before I enable the web service.

It just uses a simple view with a button to start loading the file and TextView to display the XML at the moment. I can continue my parser :)

 package com.martins.XmlParserTest import java.io.IOException; import java.io.InputStream; import android.app.Activity; import android.content.res.AssetManager; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class Main extends Activity { Button btn; TextView tvXml; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn = (Button) findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // Load XML for parsing. AssetManager assetManager = getAssets(); InputStream inputStream = null; try { inputStream = assetManager.open("textxml.xml"); } catch (IOException e) { Log.e("tag", e.getMessage()); } String s = readTextFile(inputStream); TextView tv = (TextView)findViewById(R.id.textView1); tv.setText(s); } }); } private String readTextFile(InputStream inputStream) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte buf[] = new byte[1024]; int len; try { while ((len = inputStream.read(buf)) != -1) { outputStream.write(buf, 0, len); } outputStream.close(); inputStream.close(); } catch (IOException e) { } return outputStream.toString(); } } 
+2
source share

Raises an exception due to malformed XML (line1, Pos0).

You tell the parser that the encoding is UTF-8, and if it is not, you can get various errors (depending on the parsers). If you use a non-xml editor to edit your XML file, it can save the file in a different encoding no matter what you declared in the XML document.

0
source share

All Articles