Android で SAX パーサーを使用して xml ファイルを解析する方法を学習しようとしています。
サンプルのxmlファイルを次のように配置しました: res/xml/example.xml これがxmlコードです
<?xml version="1.0" encoding="utf-8"?>
<maintag>
<item>
<name>
AndroidPeople
</name>
<website category="android" >
www.androidpeople.com
</website>
</item>
<item>
<name>
iPhoneAppDeveloper
</name>
<website category="iPhone" >
www.iphone-app-developer.com
</website>
</item>
</maintag>
これがメインのActivityクラスです
package com.me.xml_trail;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;
public class XMLParsingExample extends Activity
{
/** Create Object For SiteList Class */
SitesList sitesList = null;
SAXParserFactory spf;
SAXParser sp;
XMLReader xr;
InputSource is;
MyXMLHandler myXMLHandler;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
/** Create a new layout to display the view */
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(1);
/** Create a new textview array to display the results */
TextView name[];
TextView website[];
TextView category[];
try
{
/** Handling XML */
spf = SAXParserFactory.newInstance();
}
catch (Exception e)
{
Log.d("mytag","1 " + e);
}
try
{
sp = spf.newSAXParser();
}
catch (Exception e)
{
Log.d("mytag","2 " + e);
}
try
{
xr = sp.getXMLReader();
}
catch (Exception e)
{
Log.d("mytag","3 " + e);
}
try
{
/** Send URL to parse XML Tags */
is = new InputSource(getResources().openRawResource(R.xml.example));
is.setEncoding("utf-8");
}
catch (Exception e)
{
Log.d("mytag","4 " + e);
}
try
{
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
myXMLHandler = new MyXMLHandler();
}
catch (Exception e)
{
Log.d("mytag","5 " + e);
}
try
{
xr.setContentHandler(myXMLHandler);
}
catch (Exception e)
{
Log.d("mytag","6 " + e);
}
try
{
xr.parse(new InputSource(is.getByteStream()));
}
catch (Exception e)
{
Log.d("mytag","7 " + e);
}
/** Get result from MyXMLHandler SitlesList Object */
sitesList = MyXMLHandler.sitesList;
/** Assign textview array lenght by arraylist size */
name = new TextView[sitesList.getName().size()];
website = new TextView[sitesList.getName().size()];
category = new TextView[sitesList.getName().size()];
/** Set the result text in textview and add it to layout */
for (int i = 0; i < sitesList.getName().size(); i++)
{
name[i] = new TextView(this);
name[i].setText("Name = " + sitesList.getName().get(i));
website[i] = new TextView(this);
website[i].setText("Website = " + sitesList.getWebsite().get(i));
category[i] = new TextView(this);
category[i].setText("Website Category = " + sitesList.getCategory().get(i));
layout.addView(name[i]);
layout.addView(website[i]);
layout.addView(category[i]);
}
/** Set the layout view to display */
setContentView(layout);
}
}
ここに私のlogcatがあります
cscCountry is not German : BTU
7 org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 0: not well-formed (invalid token)
threadid=1: thread exiting with uncaught exception (group=0x40018578)
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.me.xml_trail/com.me.xml_trail.XMLParsingExample}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.me.xml_trail.XMLParsingExample.onCreate(XMLParsingExample.java:113)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
... 11 more
問題は xr.parse(new InputSource(is.getByteStream())); の行にあります。なぜ私はこれを得るのですか