Hi guys I am having an issue with xmlPullParser. I am trying to follow along with the google developers tutorial with a slight modification. Iam reaing the xml file from the raw resource file. the file is called employee.xml
and it looks like below.
<?xml version="1.0" encoding="utf-8"?>
<employees>
<employee>
<id>1</id>
<firstName>John</firstName>
<lastName>Doe</lastName>
<title>CEO</title>
<city>San Francisco, CA</city>
<managerId>0</managerId>
<department>Corporate</department>
<officePhone>123-456-0001</officePhone>
<mobilePhone>987-654-1234</mobilePhone>
<email>John@mail.com</email>
<picture>placeholder.jpg</picture>
</employee>
<employee>
<id>2</id>
<firstName>Barack</firstName>
<lastName>Obama</lastName>
<title>President</title>
<city>Washington DC</city>
<department>Corporate</department>
<managerId>0</managerId>
<officePhone>123-456-0002</officePhone>
<mobilePhone>781-000-0002</mobilePhone>
<email>Barack@mail.com</email>
<picture>barack_obama.jpg</picture>
</employee>
<employee>
<id>3</id>
<firstName>Joe</firstName>
<lastName>Biden</lastName>
<title>VP</title>
<city>Washington DC</city>
<managerId>2</managerId>
<department>Corporate</department>
<officePhone>123-456-0003</officePhone>
<mobilePhone>987-654-1234</mobilePhone>
<email>Joe@mail.com</email>
<picture>joe_biden.jpg</picture>
</employee>
<employee>
<id>4</id>
<firstName>Hillary</firstName>
<lastName>Clinton</lastName>
<title>Secretary of State</title>
<city>Washington DC</city>
<managerId>2</managerId>
<department>Corporate</department>
<officePhone>123-456-0004</officePhone>
<mobilePhone>987-654-1234</mobilePhone>
<email>Hillary@mail.com</email>
<picture>hillary_clinton.jpg</picture>
</employee>
</employees>
but when I try to pasre it in my code iam getting the exception. below is my code for parsing this xml file that I call from the main activity
public List<Employee> parse(Context cont) throws XmlPullParserException, IOException
{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
parser = factory.newPullParser();
InputStream stream = cont.getResources().openRawResource(R.raw.employee);
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(stream, null);
parser.next();
return readFeed(parser);
}
private List<Employee> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException
{
employeelist = new ArrayList<>();
Log.d("current tag: ", parser.getName());
Log.d("TAG", "The event type is: " + parser.getEventType());
parser.require(XmlPullParser.START_TAG, parser.getName().toString(), "employees");
Log.d("current tag : ", parser.getName());
while(parser.next() != XmlPullParser.END_TAG)
{
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if(name.equals("employee"))
{
employeelist.add(readEntry(parser));
}
else
{
//skip unwanted entries!!
skip(parser);
}
}
return employeelist;
}
the xmlpullparserexception is thrown when I call "parser.require()" in the readFeed() method. I am not sure why as it seems to be finding the correct "employees" tag!! as I do call parser.next() before readFeed is called!! here is the stacktrace.
11-18 08:37:51.476 4660-4676/? D/current tag:: employees
11-18 08:37:51.476 4660-4676/? D/TAG: The event type is: 2
11-18 08:37:51.477 4660-4676/? W/System.err: org.xmlpull.v1.XmlPullParserException: expected: START_TAG {employees}employees (position:START_TAG <employees>@2:12 in java.io.InputStreamReader@41f7ff40)
11-18 08:37:51.477 4660-4676/? W/System.err: at org.kxml2.io.KXmlParser.require(KXmlParser.java:2056)
11-18 08:37:51.477 4660-4676/? W/System.err: at com.example.eoin_a.xmlparserexample.EmployeeXmlParser.readFeed(EmployeeXmlParser.java:57)
11-18 08:37:51.477 4660-4676/? W/System.err: at com.example.eoin_a.xmlparserexample.EmployeeXmlParser.parse(EmployeeXmlParser.java:48)
11-18 08:37:51.477 4660-4676/? W/System.err: at com.example.eoin_a.xmlparserexample.MainActivity$1.run(MainActivity.java:55)
11-18 08:37:51.477 4660-4676/? W/System.err: at java.lang.Thread.run(Thread.java:841)
11-18 08:37:51.479 4660-4660/? D/AndroidRuntime: Shutting down VM
11-18 08:37:51.479 4660-4660/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41670d40)
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: FATAL EXCEPTION: main
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: Process: com.example.eoin_a.xmlparserexample, PID: 4660
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.eoin_a.xmlparserexample/com.example.eoin_a.xmlparserexample.MainActivity}: java.lang.NullPointerException
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:139)
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136)
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5086)
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: Caused by: java.lang.NullPointerException
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: at com.example.eoin_a.xmlparserexample.MainActivity.onCreate(MainActivity.java:38)
I included from the first debug log message also as I thought it looked unusual. it outputs 2 colons instead of 1. I don't know why!! If anyone could help me out with understanding the exception that would be great as at the minute I don't understand the exception below or what is causing it. thanks!
org.xmlpull.v1.XmlPullParserException: expected: START_TAG {employees}employees (position:START_TAG <employees>@2:12 in java.io.InputStreamReader@41f7ff40)
Here's a link to the parser.require() documentation.
public abstract void require (int type, String namespace, String name)
The middle one here is what I think is causing your problem. parser.require
is looking for the correct tag type, the correct namespace and the correct tag name; it will throw an exception if one of these doesn't match.
In your case, for the middle argument you're using getName to get the name of the current tag ("employees") and asking parser.require() to check if that matches the namespace. Namespace is a different thing to the tag name, so that will throw an exception.
You don't need to use getName
yourself when using parser.require()
you just need to pass in the name you expect.
According to the documentation "null will match any namespace and any name". If what you're doing is checking that you've got the <employees>
start tag, then you can just ignore the namespace part by using null
as the namespace argument:
parser.require(XmlPullParser.START_TAG, null, "employees");
This will check that it's a start tag, and that the tag name is "employees".