Advertisement
Guest User

XML READER

a guest
Aug 26th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.80 KB | None | 0 0
  1. MainActivity.java
  2. =======================
  3. package com.example.myxml;
  4.  
  5. import android.annotation.SuppressLint;
  6. import android.content.Context;
  7. import android.os.AsyncTask;
  8. import android.provider.DocumentsContract;
  9. import android.support.v7.app.AppCompatActivity;
  10. import android.os.Bundle;
  11. import android.util.Log;
  12. import android.widget.ListView;
  13.  
  14. import org.w3c.dom.Document;
  15. import org.w3c.dom.Node;
  16. import org.w3c.dom.NodeList;
  17. import org.xml.sax.InputSource;
  18. import org.xml.sax.SAXException;
  19.  
  20. import java.io.BufferedReader;
  21. import java.io.IOException;
  22. import java.io.InputStreamReader;
  23. import java.io.StringReader;
  24. import java.net.HttpURLConnection;
  25. import java.net.MalformedURLException;
  26. import java.net.URL;
  27. import java.util.ArrayList;
  28. import java.util.List;
  29.  
  30. import javax.xml.parsers.DocumentBuilder;
  31. import javax.xml.parsers.DocumentBuilderFactory;
  32. import javax.xml.parsers.ParserConfigurationException;
  33.  
  34. public class MainActivity extends AppCompatActivity {
  35.     //declate a collection of currencies, to hold each currency data
  36.     private List<ClsCur> myCurList;
  37.     //declare of xmlString which will get our string from the internet
  38.     String xmlString="";
  39.     //our beloved context
  40.     Context context;
  41.     //declare of list view in xml
  42.     ListView myListView;
  43.     public final String bankURL="https://www.boi.org.il/currency.xml";
  44.     @Override
  45.     protected void onCreate(Bundle savedInstanceState) {
  46.         super.onCreate(savedInstanceState);
  47.         setContentView(R.layout.activity_main);
  48.         setPointer();
  49.         readXML();
  50.     }
  51.  
  52.     @SuppressLint("StaticFieldLeak")
  53.     private void readXML() {
  54.         xmlString="";
  55.         //we next ever never using void , we need to use Void
  56.        new AsyncTask<Void,Void,String>(){
  57.             @Override
  58.             protected String doInBackground(Void... voids) {
  59.                 //we need to open HTTP URL connection to our desired URL (www.boi.org.il)
  60.                 HttpURLConnection connection = null;
  61.                 try {
  62.                     connection = (HttpURLConnection) new URL(bankURL).openConnection();
  63.                     BufferedReader buf = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  64.                     String line;
  65.                     while ((line=buf.readLine())!=null){
  66.                         xmlString+=line;
  67.                     }
  68.                 } catch (IOException e) {
  69.                     e.printStackTrace();
  70.                 }
  71.                 return xmlString;
  72.             }
  73.  
  74.             @Override
  75.             protected void onPostExecute(String s) {
  76.                 //Log.e("XML", "onPostExecute: "+s);
  77.                 parseXML(s);
  78.             }
  79.         }.execute();
  80.     }
  81.  
  82.     private void parseXML(String xmlData) {
  83.         //we will create a document builder that will allow us an easier access to our XML String
  84.         DocumentBuilder builder;
  85.  
  86.         try {
  87.             builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  88.             Document DOM = builder.parse(new InputSource(new StringReader(xmlData)));
  89.  
  90.             //our xml structure
  91.             NodeList itemName = DOM.getElementsByTagName("NAME");
  92.             NodeList itemUnit = DOM.getElementsByTagName("UNIT");
  93.             NodeList itemCode = DOM.getElementsByTagName("CURRENCYCODE");
  94.             NodeList itemCountry = DOM.getElementsByTagName("COUNTRY");
  95.             NodeList itemRate = DOM.getElementsByTagName("RATE");
  96.             NodeList itemChange = DOM.getElementsByTagName("CHANGE");
  97.  
  98.             //let got on our entire collection to get the data
  99.             for (int index=0;index<itemName.getLength();index++){
  100.                 myCurList.add(new ClsCur(
  101.                         itemName.item(index).getTextContent(),
  102.                         Integer.parseInt(itemUnit.item(index).getTextContent()),
  103.                         itemCode.item(index).getTextContent(),
  104.                         itemCountry.item(index).getTextContent(),
  105.                         Float.parseFloat(itemRate.item(index).getTextContent()),
  106.                         Float.parseFloat(itemChange.item(index).getTextContent())
  107.                 ));
  108.             }
  109.             /*
  110.             for (ClsCur item:myCurList){
  111.                 Log.e("cur", "parseXML: "+item.getCurrencyCode()+" "+item.getRate() );
  112.             }
  113.             */
  114.             //create adapter and assign it to the list view
  115.             CurAdapter adapter = new CurAdapter(context,myCurList);
  116.             myListView.setAdapter(adapter);
  117.         } catch (ParserConfigurationException | SAXException | IOException e) {
  118.             e.printStackTrace();
  119.         }
  120.  
  121.     }
  122.  
  123.     private void setPointer() {
  124.         this.context=this;
  125.         myCurList = new ArrayList<>();
  126.         myListView=findViewById(R.id.lstCur);
  127.     }
  128. }
  129.  
  130.  
  131. activity_main.xml
  132. ==================
  133. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  134.     android:layout_width="match_parent"
  135.     android:layout_height="match_parent"
  136.     android:orientation="vertical" >
  137.     <TextView
  138.         android:layout_width="match_parent"
  139.         android:layout_height="wrap_content"
  140.         android:textSize="32sp"
  141.         android:text="Current Currencies"
  142.         android:gravity="center"
  143.         />
  144.     <LinearLayout
  145.         android:layout_width="match_parent"
  146.         android:layout_height="3dp"
  147.         android:background="#000"
  148.         />
  149.     <ListView
  150.         android:layout_width="match_parent"
  151.         android:layout_height="match_parent"
  152.         android:id="@+id/lstCur"/>
  153. </LinearLayout>
  154.  
  155.  
  156. CurAdapter.java
  157. ==================
  158. package com.example.myxml;
  159.  
  160. import android.annotation.SuppressLint;
  161. import android.content.Context;
  162. import android.graphics.Color;
  163. import android.view.LayoutInflater;
  164. import android.view.View;
  165. import android.view.ViewGroup;
  166. import android.widget.BaseAdapter;
  167. import android.widget.TextView;
  168.  
  169. import java.util.List;
  170.  
  171. public class CurAdapter extends BaseAdapter {
  172.     Context context;
  173.     List<ClsCur> myCurList;
  174.  
  175.     public CurAdapter(Context context, List<ClsCur> myCurList) {
  176.         this.context = context;
  177.         this.myCurList = myCurList;
  178.     }
  179.  
  180.     @Override
  181.     public int getCount() {
  182.         return myCurList.size();
  183.     }
  184.  
  185.     @Override
  186.     public Object getItem(int position) {
  187.         return null;
  188.     }
  189.  
  190.     @Override
  191.     public long getItemId(int position) {
  192.         return 0;
  193.     }
  194.  
  195.     @SuppressLint("SetTextI18n")
  196.     @Override
  197.     public View getView(int position, View convertView, ViewGroup parent) {
  198.         View myView = LayoutInflater.from(context).inflate(R.layout.cur_item,null);
  199.         ((TextView)myView.findViewById(R.id.txtName)).setText(myCurList.get(position).getName());
  200.         ((TextView)myView.findViewById(R.id.txtCurCode)).setText(myCurList.get(position).getCurrencyCode());
  201.         ((TextView)myView.findViewById(R.id.txtRate)).setText(myCurList.get(position).getRate()+"");
  202.         ((TextView)myView.findViewById(R.id.txtChange)).setText(myCurList.get(position).getChange()+"");
  203.         ((TextView)myView.findViewById(R.id.txtChange)).setTextColor(myCurList.get(position).getRate()>0? Color.RED:Color.GREEN);
  204.         return myView;
  205.     }
  206. }
  207.  
  208.  
  209. cur_item.xml
  210. ================
  211. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  212.     android:layout_width="match_parent"
  213.     android:layout_height="wrap_content"
  214.     android:orientation="horizontal"
  215.     >
  216.  
  217.     <TextView
  218.         android:layout_width="match_parent"
  219.         android:layout_height="match_parent"
  220.         android:text="MOCK DATA"
  221.         android:layout_marginBottom="10dp"
  222.         android:layout_weight="1"
  223.         android:id="@+id/txtName"/>
  224.  
  225.     <TextView
  226.         android:layout_width="match_parent"
  227.         android:layout_height="match_parent"
  228.         android:text="MOCK DATA"
  229.         android:layout_marginBottom="10dp"
  230.         android:layout_weight="1"
  231.         android:id="@+id/txtCurCode"/>
  232.     <TextView
  233.         android:layout_width="match_parent"
  234.         android:layout_height="match_parent"
  235.         android:text="MOCK DATA"
  236.         android:layout_marginBottom="10dp"
  237.         android:layout_weight="1"
  238.         android:id="@+id/txtRate"/>
  239.     <TextView
  240.         android:layout_width="match_parent"
  241.         android:layout_height="match_parent"
  242.         android:text="MOCK DATA"
  243.         android:layout_marginBottom="10dp"
  244.         android:layout_weight="1"
  245.         android:id="@+id/txtChange"/>
  246. </LinearLayout>
  247.  
  248.  
  249.  
  250. ClsCur.java
  251. ================
  252. package com.example.myxml;
  253.  
  254. public class ClsCur {
  255.     String name;
  256.     int unit;
  257.     String currencyCode;
  258.     String country;
  259.     float rate;
  260.     float change;
  261.  
  262.     public ClsCur(String name, int unit, String currencyCode, String country, float rate, float change) {
  263.         this.name = name;
  264.         this.unit = unit;
  265.         this.currencyCode = currencyCode;
  266.         this.country = country;
  267.         this.rate = rate;
  268.         this.change = change;
  269.     }
  270.  
  271.     public String getName() {
  272.         return name;
  273.     }
  274.  
  275.     public void setName(String name) {
  276.         this.name = name;
  277.     }
  278.  
  279.     public int getUnit() {
  280.         return unit;
  281.     }
  282.  
  283.     public void setUnit(int unit) {
  284.         this.unit = unit;
  285.     }
  286.  
  287.     public String getCurrencyCode() {
  288.         return currencyCode;
  289.     }
  290.  
  291.     public void setCurrencyCode(String currencyCode) {
  292.         this.currencyCode = currencyCode;
  293.     }
  294.  
  295.     public String getCountry() {
  296.         return country;
  297.     }
  298.  
  299.     public void setCountry(String country) {
  300.         this.country = country;
  301.     }
  302.  
  303.     public float getRate() {
  304.         return rate;
  305.     }
  306.  
  307.     public void setRate(float rate) {
  308.         this.rate = rate;
  309.     }
  310.  
  311.     public float getChange() {
  312.         return change;
  313.     }
  314.  
  315.     public void setChange(float change) {
  316.         this.change = change;
  317.     }
  318. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement