I am working on a project that parses xml files and I am having trouble with both SAXParserFactory and DocumentBuilderFactory. For both of them when I try to call the newInstance() function, eclipse red underlines it and says "SAXParserFactory.newInstance cannot be resolved to a type." I am not sure why this is happening, perhaps I am missing something completely obvious. Anyway, here is the relevant code:
Feed.java
package xmlpac;
import java.net.*;
import java.nio.charset.MalformedInputException;
import java.util.ArrayList;
import java.io.*;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class Feed {
String urlString;
ArrayList<NewsCard> newscards;
public Feed(String urlString) {
this.urlString = urlString;
}
public void FileReader() throws MalformedInputException, IOException{
ArrayList<NewsCard> gettingCards = new ArrayList<>();
URL url = new URL(urlString);
BufferedReader stream = new BufferedReader(new InputStreamReader(url.openStream()));
SAXParserFactory factory = new SAXParserFactory.newInstance();
}
I have tried clean building eclipse, restarting and trying it in other IDEs, however they all have the same error which leads me to believe that there may be an error somewhere in the code or in my imports.
Thanks in advance for the help!
I think you have a typo:
SAXParserFactory factory = new SAXParserFactory.newInstance();
Should be:
SAXParserFactory factory = SAXParserFactory.newInstance();
You're trying to do a "new" on the result of the SAXParserFactory.newInstance();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With