D
D
dearname2014-02-05 23:03:53
Android
dearname, 2014-02-05 23:03:53

Why is the android SAX Xml parser not outputting anything?

Good afternoon, so I downloaded the parser according to this example (below). It works and displays everything as it should.
www.pcsalt.com/android/xml-parsing-using-saxparser...
But as soon as I change the address to www.cbr.ru/scripts/XML_daily.asp?date_req=02/03/2002
and also change all the variables to the necessary ones, I have it empty, it does not display anything. Although everything is by analogy. Maybe there is some fundamental snag in these two xml files?

package pcsalt.example.xmlparsingsaxdemo;

public class PostValue {
  String name, value;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getValue() {
    return value;
  }

  public void setValue(String value) {
    this.value = value;
  }


}

package pcsalt.example.xmlparsingsaxdemo;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

  TextView tvResponse;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tvResponse = (TextView) findViewById(R.id.tvResponse);
    new LoginAsync().execute();
  }

  class LoginAsync extends AsyncTask<Void, Void, Void> {
    XMLHelper helper;

    @Override
    protected Void doInBackground(Void... params) {
      helper = new XMLHelper();
      helper.get();
      return null;
    }

    @Override
    protected void onPostExecute(Void result) {

      StringBuilder builder = new StringBuilder();
      for (PostValue Valute : helper.Valutes) {
        builder.append("\nPost: " + Valute.getName());
        builder.append("\nPublish Date: " + Valute.getValue());
      }
      tvResponse.setText(builder.toString());
    }
  }

}

package pcsalt.example.xmlparsingsaxdemo;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;

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;

import android.util.Log;

public class XMLHelper extends DefaultHandler {
  private String URL_Main = "http://cbr.ru/scripts/XML_daily.asp?format=xml";
  String TAG = "XMLHelper";

  Boolean currTag = false;
  String currTagVal = "";
  public PostValue Valute= null;
  public ArrayList<PostValue> Valutes = new ArrayList<PostValue>();

  public void get() {
    try {
      SAXParserFactory factory = SAXParserFactory.newInstance();
      SAXParser parser = factory.newSAXParser();
      XMLReader reader = parser.getXMLReader();
      reader.setContentHandler(this);
      InputStream inputStream = new URL(URL_Main).openStream();
      reader.parse(new InputSource(inputStream));
      
    } catch (Exception e) {
      Log.e(TAG, "Exception: " + e.getMessage());
    }
  }

  // Receives notification of the start of an element
  @Override
  public void startElement(String uri, String localName, String qName,
      Attributes attributes) throws SAXException {
    Log.i(TAG, "TAG: " + localName);
    
    currTag = true;
    currTagVal = "";
    
    if (localName.equals("Valute")) {
      Valute = new PostValue();
    }
  }

  // Receives notification of end of element
  @Override
  public void endElement(String uri, String localName, String qName)
      throws SAXException {

    currTag = false;
    
    if (localName.equalsIgnoreCase("Name"))
      Valute.setName(currTagVal);
    
    else if(localName.equalsIgnoreCase("Value"))
      Valute.setValue(currTagVal);

    else if (localName.equalsIgnoreCase("Valute"))
      Valutes.add(Valute);

  }

  // Receives notification of character data inside an element 
  @Override
  public void characters(char[] ch, int start, int length)
      throws SAXException {

    if (currTag) {
      currTagVal = currTagVal + new String(ch, start, length);
      currTag = false;
    }

  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
evgenyrashev5, 2015-02-05
@evgenyrashev5


their xml structure

<posts>
<post>
<post_title>Заголовок</post_title>
<guid>Сылка</guid>
<post_date>Дата число</post_date>
</post>
</posts>

you don't understand)))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question