How to parse an xml to java class with recycler view or in adapter of recyclerview

后端 未结 2 520
执念已碎
执念已碎 2020-12-07 03:53

I have an xml file which name it is Bookmark and it is into the xml folder. I want to parse xml to recycler view which I can show the list of the Bookmark in recycler view.

相关标签:
2条回答
  • 2020-12-07 04:23

    You can use this library to parse XML into your POJO file. parsing can be done in your Fragment.

    0 讨论(0)
  • 2020-12-07 04:45

    UPDATE 2

    FragmentBookmark

    public class FragmentBookmark extends Fragment {
    
    
        public FragmentBookmark() {
            // Required empty public constructor
        }
    
        private Context mContext;
        ArrayList<Bookmark> arrayList = new ArrayList<>();
        XmlPullParserFactory pullParserFactory;
    
        RecyclerView myRecyclerView;
        DataAdapter dataAdapter;
    
        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
            mContext = context;
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
    
    
            View rootView = inflater.inflate(R.layout.fragment_bookmark, container, false);
    
            myRecyclerView = rootView.findViewById(R.id.myRecyclerView);
            myRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
            myRecyclerView.setHasFixedSize(true);
    
            dataAdapter = new DataAdapter(mContext, arrayList);
            myRecyclerView.setAdapter(dataAdapter);
    
            try {
    
                XmlPullParser xpp = getResources().getXml(R.xml.bookmarks);
    
                while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {
                    if (xpp.getEventType() == XmlPullParser.START_TAG) {
                        if (xpp.getName().equals("Bookmark")) {
    
                            Log.e("MY_VALUE", " * " + xpp.getAttributeValue(0) + " * ");
                            Log.e("MY_VALUE", " * " + xpp.getAttributeValue(1) + " * ");
                            Log.e("MY_VALUE", " * " + xpp.getAttributeValue(5) + " * ");
                            Log.e("MY_VALUE", " * " + xpp.getAttributeValue(2) + " * ");
                            Log.e("MY_VALUE", " * " + xpp.getAttributeValue(3) + " * ");
                            Log.e("MY_VALUE", " * " + xpp.getAttributeValue(4) + " * ");
    
    
                            Bookmark bookmark = new Bookmark();
                            bookmark.setName(xpp.getAttributeValue(0));
    
                            int drawableResourceId = this.getResources().getIdentifier(xpp.getAttributeValue(1), "drawable", mContext.getPackageName());
                            bookmark.setIcon(drawableResourceId);
    
                            bookmark.setId(xpp.getAttributeValue(2));
    
                            bookmark.setSearchUrl(xpp.getAttributeValue(3));
                            bookmark.setNativeUrl(xpp.getAttributeValue(4));
                            arrayList.add(bookmark);
    
                        }
                    }
    
                    xpp.next();
                }
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            dataAdapter.notifyDataSetChanged();
            return rootView;
        }
    
    }
    

    layout.fragment_bookmark

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/myRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
    </RelativeLayout>
    

    DataAdapter

    public class DataAdapter  extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
    
        private Context context;
        ArrayList<Bookmark> arrayList = new ArrayList<>();
    
        public DataAdapter(Context context, ArrayList<Bookmark> arrayList) {
            this.context = context;
            this.arrayList = arrayList;
        }
    
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
    
            View view=LayoutInflater.from(context).inflate(R.layout.custom_layout,parent,false);
            return new ViewHolder(view);
        }
    
        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    
            holder.tvName.setText(arrayList.get(position).getName());
    
            holder.tvIcon.setImageResource(arrayList.get(position).getIcon());
    
            holder.tvId.setText(arrayList.get(position).getId());
            holder.tvSearchUrl.setText(arrayList.get(position).getSearchUrl());
            holder.tvNativeUrl.setText(arrayList.get(position).getNativeUrl());
        }
    
        @Override
        public int getItemCount() {
            return arrayList.size();
        }
    
        public class ViewHolder extends RecyclerView.ViewHolder {
    
            TextView tvName,tvId,tvSearchUrl,tvNativeUrl;
    
            ImageView tvIcon;
    
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
    
                tvName=itemView.findViewById(R.id.tvName);
                tvIcon=itemView.findViewById(R.id.tvIcon);
                tvId=itemView.findViewById(R.id.tvId);
                tvSearchUrl=itemView.findViewById(R.id.tvSearchUrl);
                tvNativeUrl=itemView.findViewById(R.id.tvNativeUrl);
            }
        }
    }
    

    layout.custom_layout

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:cardCornerRadius="15dp"
        app:cardElevation="5dp"
        app:cardUseCompatPadding="true">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="Name  : " />
    
                <TextView
                    android:id="@+id/tvName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="" />
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="Icon  : " />
    
                <ImageView
                    android:id="@+id/tvIcon"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:padding="10dp"
                    android:text="" />
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="Id  : " />
    
                <TextView
                    android:id="@+id/tvId"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="" />
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="SearchUrl  : " />
    
                <TextView
                    android:id="@+id/tvSearchUrl"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="" />
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="NativeUrl  : " />
    
                <TextView
                    android:id="@+id/tvNativeUrl"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="" />
    
            </LinearLayout>
    
        </LinearLayout>
    
    </android.support.v7.widget.CardView>
    

    Bookmark model class

    public class Bookmark
    {
        String name,id,nativeUrl,searchUrl;
        int icon;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public int getIcon() {
            return icon;
        }
    
        public void setIcon(int icon) {
            this.icon = icon;
        }
    
        public String getNativeUrl() {
            return nativeUrl;
        }
    
        public void setNativeUrl(String nativeUrl) {
            this.nativeUrl = nativeUrl;
        }
    
        public String getSearchUrl() {
            return searchUrl;
        }
    
        public void setSearchUrl(String searchUrl) {
            this.searchUrl = searchUrl;
        }
    
        @Override
        public String toString() {
            return "Bookmark{" +
                    "name='" + name + '\'' +
                    ", icon='" + icon + '\'' +
                    ", id='" + id + '\'' +
                    ", nativeUrl='" + nativeUrl + '\'' +
                    ", searchUrl='" + searchUrl + '\'' +
                    '}';
        }
    }
    

    UPDATE

    XML FILE

    <Bookmarks>
        <Bookmark
            name="Google"
            hidden="true"
            icon="google.png"
            id="1"
            nativeUrl=""
            searchUrl="https://www.google.com" />
        <Bookmark
            name="Youtube"
            hidden=""
            icon="youtube_new.png"
            id="2"
            nativeUrl=""
            searchUrl="http://m.youtube.com" />
        <Bookmark
            name="Facebook"
            hidden=""
            icon="facebook.png"
            id="3"
            nativeUrl="facebook://"
            searchUrl="https://m.facebook.com" />
        <Bookmark
            name="Twitter"
            hidden=""
            icon="twitter.png"
            id="4"
            nativeUrl=""
            searchUrl="https://mobile.twitte.com" />
        <Bookmark
            name="Instagram"
            hidden=""
            icon="instagram.png"
            id="5"
            nativeUrl="instagram://"
            searchUrl="https:instagram.com" />
        <Bookmark
            name="Gmail"
            hidden=""
            icon="gmail.png"
            id="6"
            nativeUrl="googlemail://"
            searchUrl="https://gmail.com" />
        <Bookmark
            name="Translate"
            hidden=""
            icon="google_translate.png"
            id="7"
            nativeUrl=""
            searchUrl="https://" />
    
    </Bookmarks>
    

    Here is the code to parse XML from res/XML folder

        try {
    
            XmlPullParser xpp = getResources().getXml(R.xml.bookmarks);
    
            while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {
                if (xpp.getEventType() == XmlPullParser.START_TAG) {
                    if (xpp.getName().equals("Bookmark")) {
    
                        Log.e("MY_VALUE", " * " + xpp.getAttributeValue(0) + " * ");
                        Log.e("MY_VALUE", " * " + xpp.getAttributeValue(1) + " * ");
                        Log.e("MY_VALUE", " * " + xpp.getAttributeValue(5) + " * ");
                        Log.e("MY_VALUE", " * " + xpp.getAttributeValue(2) + " * ");
                        Log.e("MY_VALUE", " * " + xpp.getAttributeValue(3) + " * ");
                        Log.e("MY_VALUE", " * " + xpp.getAttributeValue(4) + " * ");
    
                        Bookmark bookmark = new Bookmark();
                        bookmark.setName(xpp.getAttributeValue(0));
                        bookmark.setIcon(xpp.getAttributeValue(1));
                        bookmark.setId(xpp.getAttributeValue(2));
                        bookmark.setSearchUrl(xpp.getAttributeValue(3));
                        bookmark.setNativeUrl(xpp.getAttributeValue(4));
                        arrayList.add(bookmark);
    
                    }
                }
    
                xpp.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    

    There are three types of android XML parser that we can use.

    • SAX Parsers

    • DOM Parsers

    • XMLPullParser

    You can use XmlPullParserFactory to parse your xml

    Try this here is the working code

    MainActivity

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.util.Log;
    import org.xmlpull.v1.XmlPullParser;
    import org.xmlpull.v1.XmlPullParserException;
    import org.xmlpull.v1.XmlPullParserFactory;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    
    public class MainActivity extends AppCompatActivity {
    
        ArrayList<Bookmark> arrayList = new ArrayList<>();
        XmlPullParserFactory pullParserFactory;
    
        RecyclerView myRecyclerView;
        DataAdapter dataAdapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            myRecyclerView = findViewById(R.id.myRecyclerView);
            myRecyclerView.setLayoutManager(new LinearLayoutManager(this));
            myRecyclerView.setHasFixedSize(true);
            try {
                pullParserFactory = XmlPullParserFactory.newInstance();
                XmlPullParser parser = pullParserFactory.newPullParser();
    
                InputStream in_s = getApplicationContext().getAssets().open("bookmark.xml");
                parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
                parser.setInput(in_s, null);
    
                parseXML(parser);
    
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            for (int i = 0; i < arrayList.size(); i++) {
                Log.e("OUTPUT", arrayList.get(i).toString());
            }
    
            dataAdapter = new DataAdapter(this, arrayList);
            myRecyclerView.setAdapter(dataAdapter);
        }
    
        private void parseXML(XmlPullParser parser) throws XmlPullParserException, IOException {
            ArrayList<Bookmark> countries = null;
            int eventType = parser.getEventType();
            Bookmark country = null;
    
            while (eventType != XmlPullParser.END_DOCUMENT) {
                String name;
                name = parser.getName();
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        countries = new ArrayList();
                        break;
                    case XmlPullParser.START_TAG:
                        break;
    
                    case XmlPullParser.END_TAG:
                        if (name.equals("Bookmark")) {
                            Log.e("VALUE", parser.getAttributeValue(null, "name") + "");
                            Log.e("VALUE", parser.getAttributeValue(null, "icon") + "");
                            Log.e("VALUE", parser.getAttributeValue(null, "id") + "");
                            Log.e("VALUE", parser.getAttributeValue(null, "searchUrl") + "");
                            Log.e("VALUE", parser.getAttributeValue(null, "nativeUrl") + "");
    
                            Bookmark bookmark = new Bookmark();
                            bookmark.setName(parser.getAttributeValue(null, "name"));
                            bookmark.setIcon(parser.getAttributeValue(null, "icon"));
                            bookmark.setId(parser.getAttributeValue(null, "id"));
                            bookmark.setSearchUrl(parser.getAttributeValue(null, "searchUrl"));
                            bookmark.setNativeUrl(parser.getAttributeValue(null, "nativeUrl"));
                            arrayList.add(bookmark);
                        }
                        break;
                }
                eventType = parser.next();
            }
    
    
        }
    
        private void processParsing(XmlPullParser parser) throws IOException, XmlPullParserException {
            int eventType = parser.getEventType();
            Bookmark bookmark = null;
    
    
        }
    }
    

    layout.activity_main

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/myRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
    </RelativeLayout>
    

    DataAdapter

    import android.content.Context;
    import android.support.annotation.NonNull;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    import java.util.ArrayList;
    
    public class DataAdapter  extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
    
        private Context context;
        ArrayList<Bookmark> arrayList = new ArrayList<>();
    
        public DataAdapter(Context context, ArrayList<Bookmark> arrayList) {
            this.context = context;
            this.arrayList = arrayList;
        }
    
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
    
            View view=LayoutInflater.from(context).inflate(R.layout.custom_layout,parent,false);
            return new ViewHolder(view);
        }
    
        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    
            holder.tvName.setText(arrayList.get(position).getName());
            holder.tvIcon.setText(arrayList.get(position).getIcon());
            holder.tvId.setText(arrayList.get(position).getId());
            holder.tvSearchUrl.setText(arrayList.get(position).getSearchUrl());
            holder.tvNativeUrl.setText(arrayList.get(position).getNativeUrl());
        }
    
        @Override
        public int getItemCount() {
            return arrayList.size();
        }
    
        public class ViewHolder extends RecyclerView.ViewHolder {
    
            TextView tvName,tvIcon,tvId,tvSearchUrl,tvNativeUrl;
    
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
    
                tvName=itemView.findViewById(R.id.tvName);
                tvIcon=itemView.findViewById(R.id.tvIcon);
                tvId=itemView.findViewById(R.id.tvId);
                tvSearchUrl=itemView.findViewById(R.id.tvSearchUrl);
                tvNativeUrl=itemView.findViewById(R.id.tvNativeUrl);
            }
        }
    }
    

    layout.custom_layout

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:cardCornerRadius="15dp"
        app:cardElevation="5dp"
        app:cardUseCompatPadding="true">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="Name  : " />
    
                <TextView
                    android:id="@+id/tvName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="" />
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="Icon  : " />
    
                <TextView
                    android:id="@+id/tvIcon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="" />
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="Id  : " />
    
                <TextView
                    android:id="@+id/tvId"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="" />
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="SearchUrl  : " />
    
                <TextView
                    android:id="@+id/tvSearchUrl"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="" />
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="NativeUrl  : " />
    
                <TextView
                    android:id="@+id/tvNativeUrl"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="" />
    
            </LinearLayout>
    
        </LinearLayout>
    
    </android.support.v7.widget.CardView>
    

    Bookmark model class

    public class Bookmark
    {
        String name,icon,id,nativeUrl,searchUrl;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getIcon() {
            return icon;
        }
    
        public void setIcon(String icon) {
            this.icon = icon;
        }
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getNativeUrl() {
            return nativeUrl;
        }
    
        public void setNativeUrl(String nativeUrl) {
            this.nativeUrl = nativeUrl;
        }
    
        public String getSearchUrl() {
            return searchUrl;
        }
    
        public void setSearchUrl(String searchUrl) {
            this.searchUrl = searchUrl;
        }
    
        @Override
        public String toString() {
            return "Bookmark{" +
                    "name='" + name + '\'' +
                    ", icon='" + icon + '\'' +
                    ", id='" + id + '\'' +
                    ", nativeUrl='" + nativeUrl + '\'' +
                    ", searchUrl='" + searchUrl + '\'' +
                    '}';
        }
    }
    

    for more information please check below post

    • Android - XML Parser
    • Android XML Parser – XMLPullParser
    • How to read XML File in Java (DOM Parser)
    • Java SAX Parser Example
    • Parsing XML data in Android Apps
    • Android XML Parsing using DOM Parser
    • Android XML Parsing Tutorial
    • Learning to Parse XML Data in Your Android App

    OUTPUT

    0 讨论(0)
提交回复
热议问题