Zookey90

Horoskop

Aug 3rd, 2012
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. package com.example.horoskop;
  2.  
  3. import java.io.IOException;
  4.  
  5. import org.xmlpull.v1.XmlPullParser;
  6. import org.xmlpull.v1.XmlPullParserException;
  7.  
  8. import android.app.Activity;
  9. import android.content.res.Resources;
  10. import android.content.res.XmlResourceParser;
  11. import android.os.Bundle;
  12. import android.widget.TextView;
  13.  
  14. public class AndroidXmlResourceActivity extends Activity {
  15.     /** Called when the activity is first created. */
  16.     String text, part2;
  17.     String[] parts;
  18.     String sign;
  19.     TextView tvTitle;
  20.  
  21.     HoroskopActivity ha = new HoroskopActivity();
  22.  
  23.     @Override
  24.     public void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);
  26.         setContentView(R.layout.activity_android_xml_resource);
  27.  
  28.         Bundle bundle = getIntent().getExtras();
  29.         sign = bundle.getString("sign");
  30.  
  31.         tvTitle = (TextView) findViewById(R.id.tvTitle);
  32.         tvTitle.setText("Dnevni horoskop: " + sign);
  33.         TextView myXmlContent = (TextView) findViewById(R.id.my_xml);
  34.         String stringXmlContent;
  35.         try {
  36.             stringXmlContent = getEventsFromAnXML(this, sign);
  37.             myXmlContent.setText(stringXmlContent);
  38.         } catch (XmlPullParserException e) {
  39.             // TODO Auto-generated catch block
  40.             e.printStackTrace();
  41.         } catch (IOException e) {
  42.             // TODO Auto-generated catch block
  43.             e.printStackTrace();
  44.         }
  45.     }
  46.  
  47.     public String getEventsFromAnXML(Activity activity, String sign)
  48.             throws XmlPullParserException, IOException {
  49.  
  50.         StringBuffer stringBuffer = new StringBuffer();
  51.         Resources res = activity.getResources();
  52.         XmlResourceParser xpp = res.getXml(R.xml.myxml);
  53.         xpp.next();
  54.         int eventType = xpp.getEventType();
  55.         while (eventType != XmlPullParser.END_DOCUMENT) {
  56.             // Looking for a start tag
  57.             if (eventType == XmlPullParser.START_TAG) {
  58.                 // We look for "title" tag in XML response
  59.                 if (xpp.getName().equalsIgnoreCase("title")) {
  60.                     // Once we found the "title" tag, add the text it contains
  61.                     // to our builder
  62.                     // stringBuffer.append(xpp.nextText()+"\n");
  63.                     text = xpp.nextText();
  64.                 } else if (xpp.getName().equals("description")
  65.                         && text.contains(sign)) {
  66.                     text = xpp.nextText();
  67.                     parts = text.split("<br/>");
  68.                     part2 = parts[1];
  69.                     stringBuffer.append(part2 + "\n");
  70.                 }
  71.             }
  72.  
  73.             eventType = xpp.next();
  74.         }
  75.  
  76.         // stringBuffer.append("\n--- End XML ---");
  77.         return stringBuffer.toString();
  78.     }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment