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.
Related
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
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
// //////////////////////////////////////////////////////
I ran into this bug when I was trying to implement a simple xml parser derived from Android's "Parsing XML Data" training page. As I am a new Android developer--and an extremely amateur coder--the exception that is thrown when I try to implement my own modified version does not help in directing me towards the problem.
Here is the exact XmlPullParserException that is thrown:
04-06 19:10:42.772 19279-19279/com.example.reader.app E/Main-Activity﹕ expected: START_DOCUMENT {null}articles (position:START_TAG <articles>#2:11 in java.io.InputStreamReader#41ec5dd8)
Here is my test xml file:
<?xml version="1.0" encoding="utf-8"?>
<articles>
<article>
<title>Test Article Number 1</title>
<description>This is a short description of the article.</description>
<text>This is some text.</text>
</article>
<article>
<title>Test Article Number 2</title>
<description>This is a short description of the article.</description>
<text>This is some text.</text>
</article>
</articles>
Here is the onCreate method of my MainActivity.java file that opens the inputStream and instantiates my ArticleListParser class.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
// try to access the data in the xml file
Log.d(TAG, "Starting to execute your code...");
try {
InputStream minputStream = getResources().openRawResource(R.raw.test);
ArticleListParser marticleParser = new ArticleListParser();
List mlistOfArticles = marticleParser.parse(minputStream);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
And here is my ArticleListParser.java class. The instance of this class instantiated by the MainActivity does not make it past the parse() method (i.e., does not execute any other methods which would be triggered by the return readArticles(parser); line in parse())...
package com.example.reader.app;
import android.util.Log;
import android.util.Xml;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* Created by jasondblack on 4/5/14.
*/
public class ArticleListParser {
// We don't use namespaces
private static final String ns = null;
// log tag
private static final String TAG = "ArticleListParser";
public List parse(InputStream in) throws XmlPullParserException, IOException {
try {
// get a new xml pull parser
XmlPullParser parser = Xml.newPullParser();
// indicate that this xml does not have namespaces
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
// set the input stream of the pull parser
parser.setInput(in, null);
// go to the first element
parser.nextTag();
// start reading the xml
return readArticles(parser);
} finally {
// no matter what, close the input stream
in.close();
}
}
private List readArticles(XmlPullParser parser) throws XmlPullParserException, IOException {
// create List that will be returned
List articles = new ArrayList();
parser.require(XmlPullParser.START_DOCUMENT, ns, "articles");
while(parser.next() != XmlPullParser.END_TAG) {
if(parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String thisName = parser.getName();
if(thisName.equals("article")) {
articles.add(readArticle(parser));
}
else {
skip(parser);
}
}
return articles;
}
public static class Article {
public final String title;
public final String description;
public final String text;
private Article (String thisTitle, String thisDescription, String thisText) {
this.title = thisTitle;
this.description = thisDescription;
this.text = thisText;
}
}
private Article readArticle(XmlPullParser parser) throws XmlPullParserException, IOException {
// make sure the xml parser is at the beginning of a tag
parser.require(XmlPullParser.START_TAG, ns, "article");
String title = null;
String description = null;
String text = null;
// step through xml file until you reach the end tag
while(parser.getEventType() != XmlPullParser.END_TAG) {
// if it is the start tag, continue onto next element in parser
// ??? weird because we have already required a start tag in the first line of this method
if(parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
// get the name of the element in the angled brackets
String name = parser.getName();
// check to see if it is one of the titles we want ... if so extract the contents
if(name.equals("title")) {
title = readTitle(parser);
} else if (name.equals("description")) {
description = readDescription(parser);
} else if (name.equals("text")) {
text = readText(parser);
} else {
skip(parser);
}
}
// return an Article object
return new Article (title, description, text);
}
// TODO: stopped here don't be confused by duplicate methods implement your own readTitle(), readDescription(), readText(), and extractText() [their "readTExt()" method]
// Processes title tags in the feed
private String readTitle(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "title");
String thisTitle = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "title");
return thisTitle;
}
// Processes description tags in the feed.
private String readDescription(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "description");
String thisDescription = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "description");
return thisDescription;
}
// For the tags title and summary, extracts their text values.
private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
String result = "";
// TODO: possible that this will snag on my xml because I don't have type="text" in my xml elements
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 you add the following before your line parser.require() call in readArticles(), you can see that you're not actually at the START_DOCUMENT event at that point.
int eventType = parser.getEventType();
Log.i("TAG", "The event type is: " + eventType);
while (eventType != XmlPullParser.START_DOCUMENT) {
eventType = parser.next();
Log.i("TAG", "The event type is: " + eventType);
}
After commenting out the parser.nextTag(); line in parse(), this logging shows that you're now at START_DOCUMENT. But then the require() call fails with the exception mentioned in your question. It appears that using ns, which is null, for the namespace argument isn't valid. Setting both the namespace and name arguments to null does allow the require() to succeed, but may not be what you want.
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 ;)
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