Parsing XML not parsing whole file - java

I have this XML
<CurrencyExchangeMap>
<CurrencyExchangePoint>
<Address>addr 3</Address>
<Latitude>41.6940265</Latitude>
<Longitude>44.7985044</Longitude>
</CurrencyExchangePoint>
<CurrencyExchangePoint>
<Address>addr 4</Address>
<Latitude>41.7024424</Latitude>
<Longitude>44.8058617</Longitude>
</CurrencyExchangePoint>
<CurrencyExchangePoint>
<Address>addr 5</Address>
<Latitude>41.6954418</Latitude>
<Longitude>44.7046725</Longitude>
</CurrencyExchangePoint>
</CurrencyExchangeMap>
It has 1000+ CurrencyExchangePoint But when I'm parsin them it returns ONLY 167 item. What's wrong? I've checked xml file in stylus studio and by myself and didn't find any error.
List<MapLT> mapLTs = null;
try {
XMLPullParserHandler parserHandler = new XMLPullParserHandler();
mapLTs = parserHandler.parse(getAssets().open("ltlg.xml"));
for (MapLT item : mapLTs) {
LatLng lt = new LatLng(item.getLatitude(), item.getLongitude());
String title = item.getAddress();
googleMap.addMarker(new MarkerOptions().position(lt).title(title));
}
} catch (Exception ex) {
ex.printStackTrace();
}
and my xmlpullparse class:
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
public class XMLPullParserHandler {
List<MapLT> mapLTList;
private MapLT mapLT;
private String text;
public XMLPullParserHandler() {
mapLTList = new ArrayList<MapLT>();
}
public List<MapLT> getMapLT(){
return mapLTList;
}
public List<MapLT> parse(InputStream is){
XmlPullParserFactory factory = null;
XmlPullParser parser = null;
try{
factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
parser = factory.newPullParser();
parser.setInput(is, null);
int eventType = parser.getEventType();
while(eventType != XmlPullParser.END_DOCUMENT){
String tagname = parser.getName();
switch (eventType){
case XmlPullParser.START_TAG:
if(tagname.equalsIgnoreCase("CurrencyExchangePoint")){
mapLT = new MapLT();
}
break;
case XmlPullParser.TEXT:
text = parser.getText();
break;
case XmlPullParser.END_TAG:
if(tagname.equalsIgnoreCase("CurrencyExchangePoint")){
mapLTList.add(mapLT);
} else if(tagname.equalsIgnoreCase("Address")){
mapLT.setAddress(text);
} else if(tagname.equalsIgnoreCase("Latitude")){
mapLT.setLatitude(Float.parseFloat(text));
} else if(tagname.equalsIgnoreCase("Longitude")){
mapLT.setLongitude(Float.parseFloat(text));
}
break;
default:
break;
}
eventType = parser.next();
}
}catch (Exception ex){
ex.printStackTrace();
}
return mapLTList;
}
}

I can't spot a problem in your code, so my strategy would be to add logs.
Start by printing all the event tags and tag names you get.

Problem was that value of one tag from 6500 was empty..
And it took my 4 hour

Related

Convert BufferedReader To InputStream

I just finished writing a bunch of code that works fine when reading from internal resource :))))
try {
SeriesDataXMLPullParserHandler seriesDataXmlPullParserHandler = new SeriesDataXMLPullParserHandler();
entries = seriesDataXmlPullParserHandler.parse(getAssets().open("series_box.xml"));
} catch (IOException e) {
e.printStackTrace();
Log.d("errorOpeningSeries", e.getMessage());
}
Collections.sort(entries, new Comparator<Entry>() {
#Override
public int compare(Entry entryOne, Entry entryTwo) {
return (entryOne.getSeriesName().compareTo(entryTwo.getSeriesName()));
}
});
listView.setAdapter(new MyAdapter(this, R.id.details_SeriesName, entries));
"SeriesDataXMLPullParserHandler" class parse data from xml file that uses InputStream as argument
here is "SeriesDataXMLPullParserHandler" class
public class SeriesDataXMLPullParserHandler {
List<Entry> entries;
private Entry entry;
private String text;
public SeriesDataXMLPullParserHandler() {
entries = new ArrayList<>();
}
public List<Entry> getEntries() {
return entries;
}
public List<Entry> parse(InputStream inputStream) {
XmlPullParserFactory xmlPullParserFactory = null;
XmlPullParser xmlPullParser = null;
try {
xmlPullParserFactory = XmlPullParserFactory.newInstance();
xmlPullParserFactory.setNamespaceAware(true);
xmlPullParser = xmlPullParserFactory.newPullParser();
xmlPullParser.setInput(inputStream, null);
int eventType = xmlPullParser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
String tagname = xmlPullParser.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
if (tagname.equalsIgnoreCase("series")) {
entry = new Entry();
}
break;
case XmlPullParser.TEXT:
text = xmlPullParser.getText();
break;
case XmlPullParser.END_TAG:
if (tagname.equalsIgnoreCase("series")) {
entries.add(entry);
} else if (tagname.equalsIgnoreCase("id")) {
entry.setId(text);
} else if (tagname.equalsIgnoreCase("Actors")) {
entry.setActors(text);
}else if (tagname.equalsIgnoreCase("Genre")) {
entry.setGenre(text);
} else if (tagname.equalsIgnoreCase("IMDB_ID")) {
entry.setImdb_id(text);
} else if (tagname.equalsIgnoreCase("Language")) {
entry.setLanguage(text);
} else if (tagname.equalsIgnoreCase("Network")) {
entry.setNetwork(text);
} else if (tagname.equalsIgnoreCase("NetworkID")) {
entry.setNetwork_id(text);
} else if (tagname.equalsIgnoreCase("Overview")) {
entry.setOverview(text);
} else if (tagname.equalsIgnoreCase("SeriesID")) {
entry.setSeriesId(text);
} else if (tagname.equalsIgnoreCase("SeriesName")) {
entry.setSeriesName(text);
}
break;
default:
break;
}
eventType = xmlPullParser.next();
}
} catch (XmlPullParserException | IOException e) {
e.printStackTrace();
}
return entries;
}
}
but the problem is when I want to get data from server, it comes in "InputStreamReader" type
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()));
the question is how do I change the type of "BufferedReader" to "InputStream" for parsing data???
or the best way to do such a thing ???
sorry for bad english :)

XML parsing converting string to int

I made a string that contains part of xml that I am working on.
There are some integers in that file and I can not covert them from String to int. I try with getAttributeValue(0) but it didn`t work. So I was wondering if someone may have a solution for my problem. Thanks
import android.os.AsyncTask;
import android.util.Log;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.URL;
class ReadXMLFile extends AsyncTask<Void, Void, Boolean> {
#Override
protected Boolean doInBackground(Void... params) {
Log.d("beginning: ", "Pocetak");
//TODO - Call getFeeds Method to populate feeds list & return true/false depending on result of operation
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
String xmldata = <cenovnik>
<cenovnikStavkaList>
<id>12195432</id>
<izlaznaStanica>100</izlaznaStanica>
<kategorijaVozila>0</kategorijaVozila>
<ulaznaStanica>100</ulaznaStanica>
</cenovnikStavkaList>
<cenovnikStavkaList>
<id>12197782</id>
<izlaznaStanica>100</izlaznaStanica>
<kategorijaVozila>1</kategorijaVozila>
<ulaznaStanica>100</ulaznaStanica>
</cenovnikStavkaList>
xpp.setInput(new StringReader( xmldata ));
Log.d("step1a", "string");
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
String tagname = xpp.getName();
if(eventType == XmlPullParser.START_DOCUMENT) {
Log.d("Step 2a: ","Start document");
} else if(eventType == XmlPullParser.START_TAG) {
Log.d("Step 2b: ", "Start tag " + xpp.getName());
if(xpp.getName().equals("cenovnikStavkaList")){
String testid = xpp.getAttributeValue(null, "id");
Log.d("Step 2d - ID: ", testid);
}
} else if(eventType == XmlPullParser.END_TAG) {
} else if(eventType == XmlPullParser.TEXT) {
Log.d("Step 2d: ", xpp.getText());
}
eventType = xpp.next();
}
Log.d("Step 2e: ", "End document");
Log.d("Step 2: ", "radi");
return true;
} catch (Exception e) {
return false;
}
}
#Override
protected void onPostExecute(Boolean result) {
if(result){
Log.d("END TEST","done");
}
}
public static InputStream getInputStream(URL url) {
try {
return url.openConnection().getInputStream();
} catch (IOException e) {
return null;
}
}
}
The numbers are inside leaf tags, like <id>12195432</id>.
The XML parsing events needs states, keeping data in variables.
Officially you could have several TEXTs inside two tags, hence:
StringBuilder text;
} else if(eventType == XmlPullParser.START_TAG) {
text = new StringBuilder();
} else if(eventType == XmlPullParser.TEXT) {
if (text != null) {
text.append(xpp.getText());
}
} else if(eventType == XmlPullParser.END_TAG) {
if (text != null) {
String s = text.toString();
try {
int n = Integer.parseInt(s);
// do something with number n
} catch (NumberFormatException e) {
// No number
}
text = null;
}
}
Create a new field cenovnikStavkaList with itself a field id for every same named tag, and do
cenovnikStavkaList.id = n;
and on END_TAG add it to a list.

XMLStreamException: ParseError at [row,col]:[5,3] Message: The element type "meta" must be terminated by the matching end-tag "</meta>"

I am trying to display a text from a news in any category of a newspaper. I use RSS of this newspaper. However when I run the code, sometimes I get the exception message in the above, sometimes it works correctly. Here is my RSS Parser code:
And the rss page that I use is:
RSSFeedParser parser = new RSSFeedParser("http://www.cumhuriyet.com.tr/rss/5");
Feed feed = parser.readFeed();
The RSS parser code:
package main;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.XMLEvent;
public class RSSFeedParser {
static final String TITLE = "title";
static final String DESCRIPTION = "description";
static final String CHANNEL = "channel";
static final String LANGUAGE = "language";
static final String COPYRIGHT = "copyright";
static final String LINK = "link";
static final String AUTHOR = "author";
static final String ITEM = "item";
static final String PUB_DATE = "pubDate";
static final String GUID = "guid";
static final String IMG = "img";
final URL url;
public RSSFeedParser(String feedUrl) {
try {
this.url = new URL(feedUrl);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
public Feed readFeed() {
Feed feed = null;
try {
boolean isFeedHeader = true;
// Set header values intial to the empty string
String description = "";
String title = "";
String link = "";
String language = "";
String copyright = "";
String author = "";
String pubDate = "";
String guid = "";
String img = "";
// First create a new XMLInputFactory
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
// Setup a new eventReader
InputStream in = read();
XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
// read the XML document
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
if (event.isStartElement()) {
String localPart = event.asStartElement().getName()
.getLocalPart();
switch (localPart) {
case ITEM:
if (isFeedHeader) {
isFeedHeader = false;
feed = new Feed(title, link, description, language,
copyright, pubDate);
}
event = eventReader.nextEvent();
break;
case TITLE:
title = getCharacterData(event, eventReader);
break;
case DESCRIPTION:
description = getCharacterData(event, eventReader);
break;
case LINK:
link = getCharacterData(event, eventReader);
break;
case GUID:
guid = getCharacterData(event, eventReader);
break;
case LANGUAGE:
language = getCharacterData(event, eventReader);
break;
case AUTHOR:
author = getCharacterData(event, eventReader);
break;
case PUB_DATE:
pubDate = getCharacterData(event, eventReader);
break;
case COPYRIGHT:
copyright = getCharacterData(event, eventReader);
break;
}
} else if (event.isEndElement()) {
if (event.asEndElement().getName().getLocalPart() == (ITEM)) {
FeedMessage message = new FeedMessage();
message.setDescription(description);
message.setPubDate(pubDate);
message.setLink(link);
message.setTitle(title);
message.setImg(img);
feed.getMessages().add(message);
event = eventReader.nextEvent();
continue;
}
}
}
} catch (XMLStreamException e) {
throw new RuntimeException(e);
}
return feed;
}
private String getCharacterData(XMLEvent event, XMLEventReader eventReader)
throws XMLStreamException {
String result = "";
event = eventReader.nextEvent();
if (event instanceof Characters) {
result = event.asCharacters().getData();
}
return result;
}
private InputStream read() {
try {
return url.openStream();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Exception message is:
Exception in thread "main" java.lang.RuntimeException: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[5,3]
Message: The element type "meta" must be terminated by the matching end-tag "</meta>".
at main.RSSFeedParser.readFeed(RSSFeedParser.java:112)
at cumhuriyet.Dunya.cumDunya(Dunya.java:32)
at automation.ServerInteraction.main(ServerInteraction.java:83)
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[5,3]
Message: The element type "meta" must be terminated by the matching end-tag "</meta>".
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source)
at com.sun.xml.internal.stream.XMLEventReaderImpl.nextEvent(Unknown Source)
at main.RSSFeedParser.readFeed(RSSFeedParser.java:58)
... 2 more
That looks like it is a live document; i.e. one that changes fairly frequently. There is also no sign of a tag in it.
I can think of two explanations for what is happening:
Sometimes the document is being generated or created incorrectly.
Sometimes you are getting an HTML error page instead of the document
you are expecting, and the XML parser can't cope with a tag in
the HTML's .
To track this down, you are going to have to capture the precise input that is causing the parse to fail.

java.lang NullPointerException in android

I am making an android app that downloads an xml file and creates a list view where the downloaded data are shown. The app was working fine. Suddenly the screen appears empty. I did not make any change. I have a variable that shows the length of the arrayadapter and it appears to be 0. The logcat shows a NullPointerException in the file below:
package com.makemyandroidapp.example.stacksites;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import android.content.Context;
public class SitesXmlPullParser {
/*static final String KEY_SITE = "site";
static final String KEY_NAME = "name";
static final String KEY_LINK = "link";
static final String KEY_ABOUT = "about"; */
static final String KEY_SITE = "pozicioni";
static final String KEY_KOMPANIA = "kompania";
static final String KEY_POZICIONI = "pozicioni";
static final String KEY_KATEGORIA = "kategoria";
static final String KEY_QYTETI = "qyteti";
static final String KEY_IMAGE_URL = "image";
public static List<StackSite> getStackSitesFromFile(Context ctx) {
// List of StackSites that we will return
List<StackSite> stackSites;
stackSites = new ArrayList<StackSite>();
// temp holder for current StackSite while parsing
StackSite curStackSite = null;
// temp holder for current text value while parsing
String curText = "";
try {
// Get our factory and PullParser
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xpp = factory.newPullParser();
// Open up InputStream and Reader of our file.
FileInputStream fis = ctx.openFileInput("StackSites.xml");
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
// point the parser to our file.
xpp.setInput(reader);
// get initial eventType
int eventType = xpp.getEventType();
boolean done = false;
int count=0;
// Loop through pull events until we reach END_DOCUMENT
while (eventType != XmlPullParser.END_DOCUMENT) {
// Get the current tag
String tagname = xpp.getName();
// React to different event types appropriately
switch (eventType) {
case XmlPullParser.START_TAG:
if (tagname.equalsIgnoreCase(KEY_SITE)&&count==0) {
// If we are starting a new <site> block we need
//a new StackSite object to represent it
curStackSite = new StackSite();
count=1;
System.out.println(count);
}
break;
case XmlPullParser.TEXT:
{String s="";
String href="";
s=xpp.getText(); //in case of cdsect this is null otherwise you get the text right here already
if (s==null) { //what would happen if it encounters in fact a cdsect
int event=xpp.nextToken(); //this is the main technical important line
if (event==XmlPullParser.CDSECT) {
s=xpp.getText();
}
}
curText=s;
break;}
case XmlPullParser.END_TAG:
if (tagname.equalsIgnoreCase(KEY_SITE)&&count==1) {
// if </site> then we are done with current Site
// add it to the list.
count=0;
System.out.println(curText);
curStackSite.setPozicioni(curText);
}
else if (tagname.equalsIgnoreCase(KEY_SITE)&&count==0){
stackSites.add(curStackSite);
} else if (tagname.equalsIgnoreCase(KEY_KOMPANIA)) {
// if </name> use setName() on curSite
//curStackSite.setKompania(curText);
System.out.println(curText+"kooooooooooooooooooooooooooooooooooooooot");
curStackSite.setKompania(curText);
System.out.println(curText+"kooooooooooooooooooooooooooooooooooooooot");
} else if (tagname.equalsIgnoreCase(KEY_KATEGORIA)) {
// if </about> use setAbout() on curSite
curStackSite.setKategoria(curText);
} else if (tagname.equalsIgnoreCase(KEY_QYTETI)){
curStackSite.setQyteti(curText);
}
break;
default:
break;
}
//move on to next iteration
eventType = xpp.next();
}
}
catch (Exception e) {
e.printStackTrace();
}
// return the populated list.
return stackSites;
}
}
NullPointerException occurs in this line:
curStackSite.setKompania(curText);
Please help me! Thanks in advance!
I'd say either to try and instancing an object and then "nulling it", before your try statement or making sure that
curStackSite = new StackSite();
Is reachable
You have it within a case within switch, so it may not always come to it. Which means the reference may fail if the object wasn't instanced.

Java inner class new instance not being created

I have a Java class that is going to have a number of inner classes. This is done for organization and to keep things in a separate file.
public class PUCObjects
{
public static class PUCNewsItem
{
public String title;
public String summary;
public String body;
public String url;
public String imageUrl;
}
}
I am then trying to create a new instance of that inner class (doing this in another class that parses some remote XML), but for some reason it doesn't seem to get created:
public static ArrayList<PUCObjects.PUCNewsItem> getPUCNews() throws IOException {
String url = "http://api.puc.edu/news/list?key="+API_KEY+"&count=30";
InputStream is = downloadUrl(url);
XmlPullParserFactory pullParserFactory;
try {
pullParserFactory = XmlPullParserFactory.newInstance();
XmlPullParser parser = pullParserFactory.newPullParser();
parser.setInput(is, null);
ArrayList<PUCObjects.PUCNewsItem> items = null;
int eventType = parser.getEventType();
PUCObjects.PUCNewsItem item = null;
Log.d("Debug: ", "Start: "+url);
while (eventType != XmlPullParser.END_DOCUMENT){
String name = null;
switch (eventType){
case XmlPullParser.START_DOCUMENT:
items = new ArrayList<PUCObjects.PUCNewsItem>();
break;
case XmlPullParser.START_TAG:
name = parser.getName();
//Log.d("Start Tag Name: ", parser.getName()+" === "+name);
if (name == "item"){
Log.d("Debug: ", "Item");
item = new PUCObjects.PUCNewsItem();
} else if (item != null){
Log.d("Debug: ", "Item is not NULL 2");
if (name == "title"){
Log.d("Title: ", parser.nextText());
item.title = parser.nextText();
} else if (name == "summary"){
item.summary = parser.nextText();
} else if (name == "body_text"){
item.body = parser.nextText();
}
}
break;
case XmlPullParser.END_TAG:
name = parser.getName();
if (name.equalsIgnoreCase("item") && item != null) {
Log.d("Debug: ", "ADD ITEM");
items.add(item);
}
break;
}//end switch
eventType = parser.next();
}//end while
Log.d("Debug: ", "Done");
return items;
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}//end
I am trying to create the object like item = new PUCObjects.PUCNewsItem(); but it seems to always be null.
Is there a reason why this is object isn't getting created?
Problem is String comparison. Your if statement is not resulting to true due to == check.
if (name == "item"){
You need to use equals() method instead of == when comparing Objects/Strings. Read this thread for more information on eqauals() vs ==

Categories

Resources