androidsaxparser

Android SAXParser cannot resolve symbol 'newInstance()'


I'm trying to learn XML parsing by using SAXParser, I've writed simple code based on tutorial, app reading xml file from URL and showing current temperature in selected city, but android studio shows me an error in line:SAXParserFactory spf = new SAXParserFactory.newInstance(); with message Cannot resolve symbol 'newInstance()' and when i try to run app throws error cannot find symbol class newInstance

I've searched all over and everybody uses the same code with .newInstance(); after SAXParserFactory(); . I know that SAXPArser is pretty old and all tutorials/articles about SAX are from 2011/12, so maybe something changed since then? someone could give me a clue what could be wrong?

here is my main java class:

<pre>package com.mycompany.weatherxmlparser;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import java.net.URL;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;


public class MainActivity extends Activity {

    String baseURL = "http://api.openweathermap.org/data/2.5/weather?q=";
    String koncowkaURL = "&mode=xml";
    TextView tv;
    EditText city;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button b = (Button) findViewById(R.id.button);
        tv = (TextView)findViewById(R.id.txtview);
        city = (EditText)findViewById(R.id.etcity);


        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //pobieranie wpisanego miasta i stanu/kraju
                String c = city.getText().toString();


                //laczenie adresu url
                StringBuilder URL = new StringBuilder(baseURL);
                URL.append(c + koncowkaURL);
                String fullURL = URL.toString();

                try {
                    URL website = new URL(fullURL);
                    //ustawianie xmlreadera do pobierania danych
                    SAXParserFactory spf = new SAXParserFactory.newInstance();
                    SAXParser sp = spf.newSAXParser();
                    XMLReader xr = sp.getXMLReader();

                    //ustawianie handlera
                    HandlingXMLStuff doingWork = new HandlingXMLStuff();
                    xr.setContentHandler(doingWork);
                    //parsowanie xml'a
                    xr.parse(new InputSource(website.openStream()));
                    //ustawianie tekstu
                    String information = doingWork.getInformation();
                    tv.setText(information);



                } catch (Exception e){
                    tv.setText("error!");
                }


            }
        });
    }

<code>

Solution

  • Make sure you are using Java JDK 7. Clean & rebuild your code, try closing & re-opening your project and restarting your IDE. Sometimes these errors happen for no good reason.