Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.horoskop;
- import java.io.IOException;
- import org.xmlpull.v1.XmlPullParser;
- import org.xmlpull.v1.XmlPullParserException;
- import android.app.Activity;
- import android.content.res.Resources;
- import android.content.res.XmlResourceParser;
- import android.os.Bundle;
- import android.widget.TextView;
- public class AndroidXmlResourceActivity extends Activity {
- /** Called when the activity is first created. */
- String text, part2;
- String[] parts;
- String sign;
- TextView tvTitle;
- HoroskopActivity ha = new HoroskopActivity();
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_android_xml_resource);
- Bundle bundle = getIntent().getExtras();
- sign = bundle.getString("sign");
- tvTitle = (TextView) findViewById(R.id.tvTitle);
- tvTitle.setText("Dnevni horoskop: " + sign);
- TextView myXmlContent = (TextView) findViewById(R.id.my_xml);
- String stringXmlContent;
- try {
- stringXmlContent = getEventsFromAnXML(this, sign);
- myXmlContent.setText(stringXmlContent);
- } catch (XmlPullParserException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public String getEventsFromAnXML(Activity activity, String sign)
- throws XmlPullParserException, IOException {
- StringBuffer stringBuffer = new StringBuffer();
- Resources res = activity.getResources();
- XmlResourceParser xpp = res.getXml(R.xml.myxml);
- xpp.next();
- int eventType = xpp.getEventType();
- while (eventType != XmlPullParser.END_DOCUMENT) {
- // Looking for a start tag
- if (eventType == XmlPullParser.START_TAG) {
- // We look for "title" tag in XML response
- if (xpp.getName().equalsIgnoreCase("title")) {
- // Once we found the "title" tag, add the text it contains
- // to our builder
- // stringBuffer.append(xpp.nextText()+"\n");
- text = xpp.nextText();
- } else if (xpp.getName().equals("description")
- && text.contains(sign)) {
- text = xpp.nextText();
- parts = text.split("<br/>");
- part2 = parts[1];
- stringBuffer.append(part2 + "\n");
- }
- }
- eventType = xpp.next();
- }
- // stringBuffer.append("\n--- End XML ---");
- return stringBuffer.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment