I try to read/parse rss feed from NASA image of the day.Here is code below. I get the exception which tell me this:
com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(Unknown Source)at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(Unknown Source)at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(Unknown Source)at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.arrangeCapacity(Unknown Source)at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipString(Unknown Source)at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source)at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)at Start.processFeed(Start.java:30)at Loader.main(Loader.java:12)
What am I doing wrong ?
P.S. of course I have another class with main method :)
Thanks in advance.
import java.io.InputStream;import java.net.URL;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.Attributes;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import org.xml.sax.XMLReader;import org.xml.sax.helpers.DefaultHandler;public class Start extends DefaultHandler { private String url = "http://www.nasa.gov/rss/dyn/image_of_the_day.rss"; private boolean inUrl = false; private boolean inTitle = false; private boolean inDescription = false; private boolean inItem = false; private boolean inDate = false; public void processFeed() { try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); XMLReader reader = parser.getXMLReader(); reader.setContentHandler(this); InputStream inputStream = new URL(url).openStream(); reader.parse(new InputSource(inputStream)); } catch(Exception e) { e.printStackTrace(); } } // processFeed @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if(localName.startsWith("item")) { inItem = true; } else if (inItem) { if(localName.equals("title")) { inTitle = true; } else { inTitle = false; } if(localName.equals("description")) { inDescription = true; } else { inDescription = false; } if(localName.equals("pubDate")) { inDate = true; } else { inDate = false; } }}@Overridepublic void characters(char[] ch, int start, int length) throws SAXException { String chars = new String(ch).substring(start, start + length); if(inTitle) { System.out.println(chars); } if(inDescription) { System.out.println(chars); } if(inDate) { System.out.println(chars); }}
}