Start XML parser - java

I am very new to Java/Android programming and need a little help with a part of code that I am writing. I have been following the "Parsing XML Data" article with no success.
The intent from "MainActivity.java" sends the string with XML text to my "ParsingXMLStringActivity.java" that I like to parse. Here is the string I successfully get over to the activity.
<action><app>survo</app><parameters><id>5666</id><p_t>205</p_t></parameters></action>
Now if wrote the public class XmlParser within the following "ParsingXMLStringActivity.java"
public class ParsingXMLStringActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parsing_xmlstring);
// Get the message from the intent
Intent intent = getIntent();
String receivedXMLstring = intent.getStringExtra(MainActivity.Authorize.XML_STRING);
System.out.println("STRING:"+receivedXMLstring);
InputStream in_stream;
try {
in_stream = new ByteArrayInputStream(receivedXMLstring.getBytes("UTF-8"));
System.out.println("STREAM:"+ in_stream);
XmlParser XmlParser = new XmlParser();
System.out.println("XmlParser:"+ XmlParser);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} // OnCreate
public class XmlParser {
public final String ns = null;
public List<Entry> parse (InputStream in_stream) throws XmlPullParserException, IOException {
try {
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in_stream, null);
parser.nextTag();
return readAction(parser);
} finally {
in_stream.close();
}
} // Public List Parse
private List<Entry> readAction(XmlPullParser parser) throws XmlPullParserException, IOException {
List<Entry> action = new ArrayList<Entry>();
parser.require(XmlPullParser.START_TAG, ns, "action");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
// Starts by looking for the entry tag
if (name.equals("action")) {
action.add(readParameters(parser));
} else {
skip(parser);
}
}
System.out.println("ACTION: "+action);
return action;
} // Public List ReadAction
private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
if (parser.getEventType() != XmlPullParser.START_TAG) {
throw new IllegalStateException();
}
int depth = 1;
while (depth != 0) {
switch (parser.next()) {
case XmlPullParser.END_TAG:
depth--;
break;
case XmlPullParser.START_TAG:
depth++;
break;
}
}
} //Private Void Skip
public class Entry {
public final String id;
public final String pt;
private Entry(String id, String pt) {
this.id = id;
this.pt = pt;
}
} // public static class Entry
private Entry readParameters(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, "entry");
String id = null;
String pt = null;
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("id")) {
id = readId(parser);
System.out.println("ID: "+ id);
} else if (name.equals("p_t")) {
pt = readPT(parser);
System.out.println("P_T: "+ pt);
}
else {
skip(parser);
}
}
return new Entry(id, pt);
} // Private Entry
// Processes title tags in the feed.
private String readId(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "id");
String id = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "id");
System.out.println(id);
return id;
}
private String readPT(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "p_t");
String pt = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "p_t");
System.out.println(pt);
return pt;
}
private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
System.out.println(result);
return result;
}
}
The System.out.println("ID: "+ id); are not giving any information to the LogCat and I have set break points to check if the parser is even started but it does not seem to start the parsing process with the string I supply.
Does someone have an idea and can tell me what part I am missing???
Kind regards, Ben

Related

Xml parsing nested tag returns null

Hello I'm new to XML parsing and completed the google tutorial for xml parsing. In the tutorial they are using: https://stackoverflow.com/feeds/tag?tagnames=android&sort=newest
So I wanted to expand this with the information about the author of the post and I tried it but it keeps returning null whatever I use that is within the author tag (uri tag and name tag)
My file where "the search for the tag" happens
/**
* This class parses XML feeds from stackoverflow.com.
* Given an InputStream representation of a feed, it returns a List of entries,
* where each list element represents a single entry (post) in the XML feed.
*/
public class StackOverflowXmlParser {
private static final String ns = null;
// We don't use namespaces
public List<Entry> parse(InputStream in) throws XmlPullParserException, IOException {
try {
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in, null);
parser.nextTag();
return readFeed(parser);
} finally {
in.close();
}
}
private List<Entry> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
List<Entry> entries = new ArrayList<Entry>();
parser.require(XmlPullParser.START_TAG, ns, "feed");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
// Starts by looking for the entry tag
if (name.equals("entry")) {
entries.add(readEntry(parser));
}
else {
skip(parser);
}
}
return entries;
}
// This class represents a single entry (post) in the XML feed.
// It includes the data members "title," "link," and "summary."
public static class Entry
{
public final String rating;
public final String title;
public final String link;
public final String author;
private Entry(String rating, String title, String link, String author)
{
this.rating = rating;
this.title = title;
this.link = link;
this.author = author;
}
}
// Parses the contents of an entry. If it encounters a title, summary, or link tag, hands them
// off
// to their respective "read" methods for processing. Otherwise, skips the tag.
private Entry readEntry(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, "entry");
String rating = null;
String title = null;
String link = null;
String author = null;
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("re:rank")){
rating = readRating_name(parser);
}
else if (name.equals("title")){
title = readTitle_name(parser);
}
else if (name.equals("id")){
link = readLink_name(parser);
}
else if (name.equals("name")){
author = readAuthor_name(parser);
}
else
{
skip(parser);
}
}
return new Entry(rating, title, link, author);
}
// Processes title tags in the feed.
private String readRating_name(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "re:rank");
String rating = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "re:rank");
return rating;
}
private String readTitle_name(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "title");
String title = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "title");
return title;
}
private String readLink_name(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "id");
String link = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "id");
return link;
}
private String readAuthor_name(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "name");
String author = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "name");
return author;
}
// For the tags title and summary, extracts their text values.
private String readText(XmlPullParser parser)
throws IOException, XmlPullParserException {
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
return result;
}
// Skips tags the parser isn't interested in. Uses depth to handle nested tags. i.e.,
// if the next tag after a START_TAG isn't a matching END_TAG, it keeps going until it
// finds the matching END_TAG (as indicated by the value of "depth" being 0).
private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
if (parser.getEventType() != XmlPullParser.START_TAG) {
throw new IllegalStateException();
}
int depth = 1;
while (depth != 0) {
switch (parser.next()) {
case XmlPullParser.END_TAG:
depth--;
break;
case XmlPullParser.START_TAG:
depth++;
break;
}
}
}
}
my class where I output the results of the parsing:
// Uploads XML from stackoverflow.com, parses it, and combines it with
// HTML markup. Returns HTML string.
private String loadXmlFromNetwork(String urlString) throws XmlPullParserException, IOException
{
InputStream stream = null;
StackOverflowXmlParser stackOverflowXmlParser = new StackOverflowXmlParser();
List<Entry> entries = null;
StringBuilder htmlString = new StringBuilder();
try {
stream = downloadUrl(urlString);
entries = stackOverflowXmlParser.parse(stream);
// Makes sure that the InputStream is closed after the app is
// finished using it.
}
finally
{
if (stream != null)
{
stream.close();
}
}
// Content section
for (Entry entry : entries)
{
// Name link
String question = getString(R.string.question);
String rating = getString(R.string.rating);
String author = getString(R.string.author);
// Question link + title
htmlString.append("<p>" + question + "<a href='" + entry.link + "'>" + entry.title + "</a><br />");
htmlString.append(rating + entry.rating + "<br />");
htmlString.append(author + entry.author + "</p>");
}
return htmlString.toString();
}
Hope it helps you, instead of xml parsing use this approach which gives json data [Easy to code]::
String xml = "xml here...";
XMLSerializer xmlSerializer = new XMLSerializer();
JSON json = xmlSerializer.read( xml );
You can get json-lib -- http://json-lib.sourceforge.net/index.html
For easily understanding XML Parsing go through this tutorial.
Its really easy to understand.
XML Parsing-Android Hive

XML Parser is never reached in onCreate

I've created an XML parser based on the Android docs example found here:
http://developer.android.com/training/basics/network-ops/xml.html
The primary difference between the docs's implementation and my own is I am attempting to implement the XML parser within my Activity - however when I attempt to do so the code does not seem to be reached although it is in onCreate (which I though would start it) I'm not sure exactly what should be done to correct this issue - but any suggestions/input is greatly appreciated.
I've tried moving the closing bracket at the end of my parser however I end up with 84 new errors (mostly duplicate local variable errors - I can provide them if necessary)
I believe I'll need to leave the bracket where it is - and simply call the parser from the android docs example somehow - but I'm not sure exactly how this should be done.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// ///////////////////////////////////////////////////////
// Read and Parse XML Data from BootConfiguration
// ///////////////////////////////////////////////////////
public List parse(InputStream in) throws XmlPullParserException,
IOException {
try {
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in, Environment.getExternalStorageDirectory()
+ "/BootConfiguration.xml");
parser.nextTag();
return readFeed(parser);
} finally {
in.close();
}
}
private List readFeed(XmlPullParser parser) throws XmlPullParserException,
IOException {
List entries = new ArrayList();
parser.require(XmlPullParser.START_TAG, ns, "feed");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
// Starts by looking for the entry tag
if (name.equals("BootConfiguration")) {
entries.add(readEntry(parser));
} else {
skip(parser);
}
}
return entries;
}
public static class Entry {
public final String url;
public final String user;
public final String password;
private Entry(String url, String user, String password) {
this.url = url;
this.user = user;
this.password = password;
}
}
// Parses the contents of an entry. If it encounters a title, summary, or
// link tag, hands them off
// to their respective "read" methods for processing. Otherwise, skips the
// tag.
private Entry readEntry(XmlPullParser parser)
throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, "BootConfiguration");
String url = null;
String user = null;
String password = null;
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("ServiceUrl")) {
url = readUrl(parser);
} else if (name.equals("ApiUser")) {
user = readUser(parser);
} else if (name.equals("ApiPassword")) {
password = readPassword(parser);
} else {
skip(parser);
}
}
return new Entry(url, user, password);
}
// Processes title tags in the feed.
private String readUrl(XmlPullParser parser) throws IOException,
XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "ServiceUrl");
String url = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "ServiceUrl");
return url;
}
// Processes title tags in the feed.
private String readUser(XmlPullParser parser) throws IOException,
XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "ApiUser");
String user = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "ApiUser");
return user;
}
// Processes summary tags in the feed.
private String readPassword(XmlPullParser parser) throws IOException,
XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "ApiPassword");
String password = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "ApiPassword");
return password;
}
// For the tags title and summary, extracts their text values.
private String readText(XmlPullParser parser) throws IOException,
XmlPullParserException {
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
return result;
}
private void skip(XmlPullParser parser) throws XmlPullParserException,
IOException {
if (parser.getEventType() != XmlPullParser.START_TAG) {
throw new IllegalStateException();
}
int depth = 1;
while (depth != 0) {
switch (parser.next()) {
case XmlPullParser.END_TAG:
depth--;
break;
case XmlPullParser.START_TAG:
depth++;
break;
}
}
// //////////////////////////////////////////////////////
// End XML Parser
// //////////////////////////////////////////////////////

Parse local XML File with XMLPullParser in Android

This is what I want to do. I wrote a class which should handle my XML File.
package de.lies;
import java.io.IOException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
public class Entry {
private static final String ns = null;
public final String rank;
public final String source;
public final String date;
public final String headline;
public final String description;
public final String articleURL;
public final String sourceURL;
public final String imageURL;
private Entry(String rank, String source, String date, String headline, String description, String articleURL, String sourceURL, String imageURL){
this.rank = rank;
this.source = source;
this.date = date;
this.headline = headline;
this.description = description;
this.articleURL = articleURL;
this.sourceURL = sourceURL;
this.imageURL = imageURL;
}
private Entry readEntry (XmlPullParser parser) throws XmlPullParserException, IOException{
parser.require(XmlPullParser.START_TAG, ns, "entry");
String rank = null;
String source = null;
String date = null;
String headline = null;
String description = null;
String articleURL = null;
String sourceURL = null;
String imageURL = null;
while(parser.next() != XmlPullParser.END_TAG) {
if(parser.getEventType() != XmlPullParser.START_TAG){
continue;
}
String name = parser.getName();
if(name.equals("rank")){
rank = readRank(parser);
} else if(name.equals("source")){
source = readSource(parser);
} else if(name.equals("date")){
date = readDate(parser);
} else if(name.equals("headline")){
headline = readHeadline(parser);
} else if(name.equals("description")){
description = readDescription(parser);
} else if(name.equals("articleURL")){
articleURL = readArticleURL(parser);
} else if(name.equals("sourceURL")){
sourceURL = readSourceURL(parser);
} else if(name.equals("imageURL")){
imageURL = readImageURL(parser);
} else {
skip(parser);
}
}
return new Entry(rank, source, date, headline, description, articleURL, sourceURL, imageURL);
}
private String readRank(XmlPullParser parser) throws IOException, XmlPullParserException{
parser.require(XmlPullParser.START_TAG, ns, "rank");
String rank = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "rank");
return rank;
}
private String readSource(XmlPullParser parser) throws IOException, XmlPullParserException{
parser.require(XmlPullParser.START_TAG, ns, "source");
String source = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "source");
return source;
}
private String readDate(XmlPullParser parser) throws IOException, XmlPullParserException{
parser.require(XmlPullParser.START_TAG, ns, "rank");
String date = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "rank");
return date;
}
private String readHeadline(XmlPullParser parser) throws IOException, XmlPullParserException{
parser.require(XmlPullParser.START_TAG, ns, "rank");
String headline = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "rank");
return headline;
}
private String readDescription(XmlPullParser parser) throws IOException, XmlPullParserException{
parser.require(XmlPullParser.START_TAG, ns, "rank");
String description = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "rank");
return description;
}
private String readArticleURL(XmlPullParser parser) throws IOException, XmlPullParserException{
parser.require(XmlPullParser.START_TAG, ns, "rank");
String articleURL = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "rank");
return articleURL;
}
private String readSourceURL(XmlPullParser parser) throws IOException, XmlPullParserException{
parser.require(XmlPullParser.START_TAG, ns, "rank");
String sourceURL = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "rank");
return sourceURL;
}
private String readImageURL(XmlPullParser parser) throws IOException, XmlPullParserException{
parser.require(XmlPullParser.START_TAG, ns, "rank");
String imageURL = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "rank");
return imageURL;
}
private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
return result;
}
private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
if (parser.getEventType() != XmlPullParser.START_TAG) {
throw new IllegalStateException();
}
int depth = 1;
while (depth != 0) {
switch (parser.next()) {
case XmlPullParser.END_TAG:
depth--;
break;
case XmlPullParser.START_TAG:
depth++;
break;
}
}
}
}
The problem I have now is how to handle the XML File. In particular: How to read in the XML-File and how to let the class do the work for the file and print it out to the Android Device. Would appreciate any help. To be more precise I have an XML file that has this layout
<entry>
<rank>Rank 1</rank>
<source><![CDATA[Der Postillon]]></source>
<date>2013-09-24 15:11:48</date>
<scores>
<Flies>10797</Flies>
<Facebook>10190</Facebook>
<Twitter>345</Twitter>
<GPlus>262</GPlus>
</scores>
<headline>Wikipedia löscht FDP-Eintrag wegen fehlender Relevanz</headline>
<description>Berlin (dpo) - Das ging schnell. Seit gestern existiert der Wikipedia-Eintrag der FDP nicht mehr. Der mit knapp 10.000 Wörtern durchaus umfangreiche Beitrag wurde nach einer kurzen Löschdiskussion entfernt, weil er den strengen Relevanzkriterien des Online-Lexikons nicht mehr standhalten konnte. Für marginale Splitterparteien und Kleinstgruppierungen wie die Liberalen sei kein Platz in der Wikipedia. [Weiterlesen]</description>
<articleURL>http://www.der-postillon.com/2013/09/wikipedia-loscht-fdp-eintrag-wegen.html</articleURL>
<sourceURL><![CDATA[http://www.der-postillon.com]]></sourceURL>
<imageURL><![CDATA[http://www.10000flies.de/images/nopic.jpg]]></imageURL>
</entry>
But I don't seem to find a good solution to handle this. But I bet this is super easy. I just can't find it. Alternatively you could give me an easy tutorial on how to read an XML file from a local folder and work with it.
if you want to get xml file from asset folder then use below code,
public void copyXMLFromAssets()
{
AssetManager assetManager = mActivity.getAssets();
File AppDirectory = new File(Environment.getExternalStorageDirectory() + "/" + dirName);
if (!AppDirectory.exists())
{
AppDirectory.mkdirs();
}
InputStream in = null;
OutputStream out = null;
try
{
in = assetManager.open(XML_name);
File outFile = new File(AppDirectory +"/"+XML_name);
out = new FileOutputStream(outFile);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
}
catch (IOException e)
{
Log.e("tag", "Failed to copy asset file: " + XML_name, e);
}
}
Then read this copied XML file from saved location using the following code
public InputStream GetStreamFromXmlFileOnSDCard()
{
File file = new File(Environment.getExternalStorageDirectory() +xml_path);
InputStream istr = null;
try
{
istr = new FileInputStream(file);
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return istr;
}
the above function returns the InputStream for xml file; you can pass this InputStream to pull parser and parse your data.

Exception during parsing XML with XMLPullParser

I need your help to parse XML announces.
Let's see the XML below :
<?xml version="1.0" encoding="UTF-8"?>
<FAVORIS>
<LOGGED>1</LOGGED>
<NOTICES NUM_PAGE="2">
<NOTICE>
<DEPARTMENT>33</DEPARTMENT>
<SOURCE>EMP</SOURCE>
<OBJET><![CDATA[Fourniture d'une solution régionale]]></OBJET>
<ID_PROCEDURE>7543</ID_PROCEDURE>
<ORGANISME><![CDATA[Gcs]]></ORGANISME>
<TYPE_AVIS>ZZ</TYPE_AVIS>
<TYPE_PROCEDURE>W3</TYPE_PROCEDURE>
<CATEGORIE>Service</CATEGORIE>
<DATE_OFFRE>16/09/2013 12:00</DATE_OFFRE>
<DECALAGE_HORAIRE>0</DECALAGE_HORAIRE>
</NOTICE>
<NOTICE>
<DEPARTMENT>33</DEPARTMENT>
<SOURCE>EMP</SOURCE>
<OBJET><![CDATA[Refonte du portail]]></OBJET>
<ID_PROCEDURE>4323</ID_PROCEDURE>
<ORGANISME><![CDATA[Mairie de W]]></ORGANISME>
<TYPE_AVIS>Z</TYPE_AVIS>
<TYPE_PROCEDURE>W1</TYPE_PROCEDURE>
<CATEGORIE>Service</CATEGORIE>
<DATE_OFFRE>03/09/2013 12:00</DATE_OFFRE>
<DECALAGE_HORAIRE>0</DECALAGE_HORAIRE>
</NOTICE>
....
</NOTICES>
</FAVORIS>
To parse this XML I followed the tutorial about XML Parsing in http://developer.android.com/training/basics/network-ops/xml.html
When I parse this XML I have an error like this :
expected: START_TAG {null}NOTICES (position:START_TAG <FAVORIS>#2:10 in java.io.InputStreamReader#40e69360)
It's due to the require but how can I resolve this issue ?
More about my code :
public class AOThumbnail_ParserXML {
private static final String ns = null;
public ArrayList<AOThumbnail> parse(String in) throws XmlPullParserException, IOException {
Decoder decoder = new Decoder();
String decoded_in = decoder.decode(in);
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(new ByteArrayInputStream(decoded_in.getBytes()), null);
parser.nextTag();
return readFeed(parser);
}
private ArrayList<AOThumbnail> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
ArrayList<AOThumbnail> entries = new ArrayList<AOThumbnail>();
parser.require(XmlPullParser.START_TAG, ns, "NOTICES");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
// Starts by looking for the entry tag
if (name.equals("NOTICE")) {
entries.add(readEntry(parser));
} else {
skip(parser);
}
}
return entries;
}
private AOThumbnail readEntry(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, "NOTICE");
String organisme = null;
String objet = null;
String date_candidature = null;
String date_offre = null;
String departement = null;
String source = null;
String idProcedure = null;
String decalageHoraire = null;
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("ORGANISME")) {
organisme = read(parser, "ORGANISME");
} else if (name.equals("OBJET")) {
objet = read(parser, "OBJET");
} else if (name.equals("DATE_CAND")) {
date_candidature = read(parser, "DATE_CAND");
} else if (name.equals("DATE_OFFRE")) {
date_offre = read(parser, "DATE_OFFRE");
} else if (name.equals("DEPARTMENT")) {
departement = read(parser, "DEPARTMENT");
} else if (name.equals("SOURCE")) {
source = read(parser, "SOURCE");
} else if (name.equals("ID_PROCEDURE")) {
idProcedure = read(parser, "ID_PROCEDURE");
} else if (name.equals("DECALAGE_HORAIRE")) {
decalageHoraire = read(parser, "DECALAGE_HORAIRE");
} else {
skip(parser);
}
}
String date = "";
try {
if (date_candidature.length() > 0)
date = date_candidature;
else if (date_offre.length() > 0)
date = date_offre;
else
date = "Se référer à l'annonce";
} catch (Exception ex) {
Logger.logit(ex);
}
return new AOThumbnail(organisme, objet, date, departement, idProcedure, source, Boolean.parseBoolean(decalageHoraire));
}
private String read(XmlPullParser parser, String balise) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, balise);
String content = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, balise);
return content;
}
private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
return result;
}
private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
if (parser.getEventType() != XmlPullParser.START_TAG) {
throw new IllegalStateException();
}
int depth = 1;
while (depth != 0) {
switch (parser.next()) {
case XmlPullParser.END_TAG:
depth--;
break;
case XmlPullParser.START_TAG:
depth++;
break;
}
}
}
}
If I parse an XML like this (see below), it works, but not with the XML above !
<NOTICES NUM_PAGE="2">
<NOTICE>
<DEPARTMENT>33</DEPARTMENT>
<SOURCE>EMP</SOURCE>
<OBJET><![CDATA[Fourniture d'une solution régionale]]></OBJET>
<ID_PROCEDURE>7543</ID_PROCEDURE>
<ORGANISME><![CDATA[Gcs]]></ORGANISME>
<TYPE_AVIS>ZZ</TYPE_AVIS>
<TYPE_PROCEDURE>W3</TYPE_PROCEDURE>
<CATEGORIE>Service</CATEGORIE>
<DATE_OFFRE>16/09/2013 12:00</DATE_OFFRE>
<DECALAGE_HORAIRE>0</DECALAGE_HORAIRE>
</NOTICE>
<NOTICE>
<DEPARTMENT>33</DEPARTMENT>
<SOURCE>EMP</SOURCE>
<OBJET><![CDATA[Refonte du portail]]></OBJET>
<ID_PROCEDURE>4323</ID_PROCEDURE>
<ORGANISME><![CDATA[Mairie de W]]></ORGANISME>
<TYPE_AVIS>Z</TYPE_AVIS>
<TYPE_PROCEDURE>W1</TYPE_PROCEDURE>
<CATEGORIE>Service</CATEGORIE>
<DATE_OFFRE>03/09/2013 12:00</DATE_OFFRE>
<DECALAGE_HORAIRE>0</DECALAGE_HORAIRE>
</NOTICE>
....
</NOTICES>
I hope you can help me,
Thanks a lot !
In readFeed() method, you are telling to your Parser that the current event should be an START_TAG with name "NOTICES", in other case the method will throw an Exception, which is the one you posted. Thats why the second .xml file works, but not the first (the first event of this file will be an START_TAG with name "FAVORIS").
So you should use another way to look up for the NOTICES block, you can use something like this (there are other ways to do it):
while (parser.next() != XmlPullParser.START_TAG && !(parser.getName().equals("NOTICES")));
So when this while loop ends, your parser will be at the start of the NOTICES block.
PS: I didn't test the code, so it might not work, but the solution should be similar.
Issue resolved using require "FAVORIS" !
private ArrayList<AOThumbnail> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
ArrayList<AOThumbnail> entries = new ArrayList<AOThumbnail>();
//Go to FAVORIS' level
parser.require(XmlPullParser.START_TAG, ns, "FAVORIS");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if(parser.getName().equals("NOTICES")) {
//Go to NOTICES' level
parser.require(XmlPullParser.START_TAG, ns, "NOTICES");
//Parse on Notices' level
return readFeed(parser);
}
// Starts by looking for the entry tag
if (name.equals("NOTICE")) {
Logger.logit("Dans notice !!!!!!");
entries.add(readEntry(parser));
} else {
skip(parser);
}
}
return entries;
}
Thanks to #javi9375 for his tips ;)

java.lang.NullPointerException Issue

I realize this is an easy issue usually, but I've been looking at other similar questions on this site and others and have not been able to fix the code, nor seeing exactly where the error is coming from. What seems to be my problem is calling an OnClickListener assigned to a button, but then again I could be wrong. I had basically straight copied this code (with minimal changes) from another application where this had worked. This fact baffles me a little bit more. Thanks in advance for your help with this relatively easy question.
Code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.update_activity);
final Handler handler;
handler = new Handler();
final EditText getCustID = (EditText) findViewById(R.id.customer);
final EditText custvar1 = (EditText) findViewById(R.id.var1);
final EditText custvar2 = (EditText) findViewById(R.id.var2);
final EditText custvar3 = (EditText) findViewById(R.id.var3);
class sendGET implements Runnable
{
private String url;
public sendGET(String mUrl)
{
url = mUrl;
}
public void run()
{
try
{
//Declare HttpClient and HttpGet
HttpClient client = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
//Run HttpGet
client.execute(httpget);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class getCustomerData implements Runnable
{
public void run()
{
try
{
InputStream stream = null;
XMLparser xmlParser = new XMLparser();
List<Customer> customers = null;
String parseUrl = URL + getCustID.getText().toString();
try
{
stream = downloadUrl(parseUrl);
customers = xmlParser.parse(stream);
}
finally
{
stream.close();
}
final Customer data = customers.get(0);
handler.post(new Runnable() {
public void run() {
custvar1.setText(String.valueOf(data.var1));
custvar2.setText(String.valueOf(data.var2));
custvar3.setText(String.valueOf(data.var3));
}
});
}
catch (IOException e)
{
//return getResources().getString(R.string.connection_error);
}
catch (XmlPullParserException e)
{
//return getResources().getString(R.string.xml_error);
}
}
private Customer readCustomer(XmlPullParser parser) throws XmlPullParserException, IOException
{
parser.require(XmlPullParser.START_TAG, null, "customer");
String custID = null;
String var1 = null;
String var2 = null;
String var3 = null;
while(parser.next() != XmlPullParser.END_TAG)
{
if (parser.getEventType() != XmlPullParser.START_TAG)
{
continue;
}
String name = parser.getName();
if (name.equals("CustID"))
{
custID = readCustID(parser);
} else if (name.equals("custvar1"))
{
var1 = readVar1(parser);
} else if (name.equals("custvar2"))
{
var2 = readVar2(parser);
} else if (name.equals("custvar3"))
{
var3 = readVar3(parser);
} else
{
skip(parser);
}
}
return new Customer(custID, var1, var2, var3);
}
private String readCustID(XmlPullParser parser) throws IOException, XmlPullParserException
{
parser.require(XmlPullParser.START_TAG, null, "CustID");
final String custID = readText(parser);
parser.require(XmlPullParser.END_TAG, null, "CustID");
return custID;
}
private String readVar1(XmlPullParser parser) throws IOException, XmlPullParserException
{
parser.require(XmlPullParser.START_TAG, null, "custvar1");
String var1 = readText(parser);
parser.require(XmlPullParser.END_TAG, null, "custvar1");
return var1;
}
private String readVar2(XmlPullParser parser) throws IOException, XmlPullParserException
{
parser.require(XmlPullParser.START_TAG, null, "custvar2");
String var2 = readText(parser);
parser.require(XmlPullParser.END_TAG, null, "custvar2");
return var2;
}
private String readVar3(XmlPullParser parser) throws IOException, XmlPullParserException
{
parser.require(XmlPullParser.START_TAG, null, "custvar3");
String var3 = readText(parser);
parser.require(XmlPullParser.END_TAG, null, "custvar3");
return var3;
}
private String readText(XmlPullParser parser) throws IOException, XmlPullParserException
{
String result = "";
if (parser.next() == XmlPullParser.TEXT)
{
result = parser.getText();
parser.nextTag();
}
return result;
}
private void skip(XmlPullParser parser) throws IOException, XmlPullParserException
{
if (parser.getEventType() != XmlPullParser.START_TAG)
{
throw new IllegalStateException();
}
int depth = 1;
while (depth != 0)
{
switch (parser.next()) {
case XmlPullParser.END_TAG:
depth --;
case XmlPullParser.START_TAG:
depth++;
break;
}
}
}
class XMLparser
{
public List<Customer> parse(InputStream in) throws XmlPullParserException, IOException {
try
{
XmlPullParser parser = Xml.newPullParser();
parser.setInput(in, null);
parser.nextTag();
return readXML(parser);
} finally {
in.close();
}
}
private List<Customer> readXML(XmlPullParser parser) throws XmlPullParserException, IOException
{
List<Customer> custData = new ArrayList<Customer>();
parser.require(XmlPullParser.START_TAG, null, "xml");
while (parser.next() != XmlPullParser.END_TAG)
{
if (parser.getEventType() != XmlPullParser.START_TAG)
{
continue;
}
String name = parser.getName();
//Look for customer tag
if (name.equals("customer"))
{
custData.add(readCustomer(parser));
}
else
{
skip(parser);
}
}
return custData;
}
}
private InputStream downloadUrl(String urlString) throws IOException
{
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
//start the query
conn.connect();
InputStream stream = conn.getInputStream();
return stream;
}
}
final Button getData = (Button) findViewById(R.id.getData);
getData.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{
//Declare and start thread
final Runnable mGetData = new getCustomerData();
final Thread getData = new Thread(mGetData);
getData.start();
}
});
final Button submit = (Button) findViewById(R.id.Submit);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//set url
String url = "**removed**";
url += "var1=" + custvar1.getText().toString() + "&var2=" + custvar2.getText().toString() + "&var3=" + custvar3.getText().toString();
//Declare and start thread
final Runnable mConnect = new sendGET(url);
final Thread Connect = new Thread(mConnect);
Connect.start();
}
});
}
public class Customer
{
public final String custID;
public final String var1;
public final String var2;
public final String var3;
private Customer()
{
custID = null;
var1 = null;
var2 = null;
var3 = null;
}
private Customer(String custID, String var1, String var2, String var3)
{
this.custID = custID;
this.var1 = var1;
this.var2 = var2;
this.var3 = var3;
}
}
Logcat Error:
01-09 19:20:53.478: W/dalvikvm(911): threadid=11: thread exiting with uncaught exception (group=0x40a13300)
01-09 19:20:53.478: E/AndroidRuntime(911): FATAL EXCEPTION: Thread-99
01-09 19:20:53.478: E/AndroidRuntime(911): java.lang.NullPointerException
01-09 19:20:53.478: E/AndroidRuntime(911): at us.rns.editdata.UpdateActivity$1getCustomerData.run(UpdateActivity.java:90)
01-09 19:20:53.478: E/AndroidRuntime(911): at java.lang.Thread.run(Thread.java:856)
01-09 19:20:54.408: W/IInputConnectionWrapper(911): showStatusIcon on inactive InputConnection
Your downloadUrl method is failing.
InputStream stream = null;
XMLparser xmlParser = new XMLparser();
List<Customer> customers = null;
String parseUrl = URL + getCustID.getText().toString();
try
{
stream = downloadUrl(parseUrl);
customers = xmlParser.parse(stream);
}
finally
{
stream.close();
}
The stream object is set to null, then you assign stream = downloadUrl(parseUrl).
The downloadUrl method fails, without having assigned anything to stream.
You then try to close the stream in the finally clause.
private InputStream downloadUrl(String urlString) throws IOException
{
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
//start the query
conn.connect();
InputStream stream = conn.getInputStream();
return stream;
}
Put a breakpoint on this method, and debug through it. Please return with the exact line it is breaking on. I suspect it is conn.connect(); or InputStream stream = conn.getInputStream();
I would also do this in the finally clause:
finally
{
if(stream != null)
stream.close();
}
It could also be the following:
You need to move all of these:
final EditText getCustID = (EditText) findViewById(R.id.customer);
final EditText custvar1 = (EditText) findViewById(R.id.var1);
final EditText custvar2 = (EditText) findViewById(R.id.var2);
final EditText custvar3 = (EditText) findViewById(R.id.var3);
final Button getData = (Button) findViewById(R.id.getData);
final Button submit = (Button) findViewById(R.id.Submit);
into onResume, rather than onCreate()

Categories

Resources