سؤال

Actually i try to do parse simple XML containing some details using SAX parser and display the result in Spinner. But i con't identify the what is the problem in my problem i mean may be it will be syntax error or my program was not correct. can anyone help me to fix this issues.

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="178dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/spinner1"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/spinner1"
        android:layout_marginTop="17dp"
        android:text="Parse XML using SAX" />

</RelativeLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/title"
    style="@style/spinner" />

styles.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="AppTheme" parent="@android:style/android:Theme" />
    <style name="spinner">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>
        <item name="android:textColor">#0000CD</item>
        <item name="android:text">15dp</item>
        <item name="android:typeface">monospace</item>
        <item name="android:maxHeight">35dp</item>
        <item name="android:paddingTop">5dp</item>
        <item name="android:paddingBottom">8dp</item>
    </style>
</resources>

Mainactivity.class

public abstract class MainActivity extends Activity implements OnClickListener,OnItemSelectedListener {

    Button button;
    Spinner spinner;
    List<Item> item = null;
    static final String URL = "http://www.androidituts.com/source/tutorials.xml"; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById();
        button.setOnClickListener(this);


    }


    private void findViewById() {
        // TODO Auto-generated method stub
        button = (Button) findViewById(R.id.button1);
        spinner = (Spinner) findViewById(R.id.spinner1);
    }

    public void onClick(View v) {

            item = SAXXMLParser.parse(URL);
            ArrayAdapter<Item> adapter = new ArrayAdapter<Item>(this,
                    R.layout.list_item, item);
            spinner.setAdapter(adapter);
            spinner.setOnItemSelectedListener(this);

    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos,
            long id) {
        Item item = (Item) parent.getItemAtPosition(pos);
        Toast.makeText(parent.getContext(), item.getDetails(),
                Toast.LENGTH_LONG).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
    }


}

Item.java

public class Item {
    private String name;
    private int id;
    private String category;
    private String published;

    public String getName() {
        return name;
    }

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

        public int getId() {
            return id;
        }


        public void setId(int id) {
            this.id = id;
        }

        public void setcategory(String category) {
             this.category = category;
        }

        public String getcategory() {
            return category;
        }

        public void setpublished(String published) {
            this.published = published;
        }


        public String getpublished() {
            return published;
        }

    public String getDetails() {
        // TODO Auto-generated method stub
           String result = id + ": " + name + "\n" + category + "-" + published;
            return result;
    }


}

SAXXMLParser.java

public class SAXXMLParser {

    public static List<Item> parse(String URL) {
        List<Item> menu=null;
           try {
                // create a XMLReader from SAXParser
                XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser()
                        .getXMLReader();
                // create a SAXXMLHandler
                SAXXMLHandler saxHandler = new SAXXMLHandler();
                // store handler in XMLReader
                xmlReader.setContentHandler(saxHandler);
                // the process starts
                xmlReader.parse(URL);
                // get the `Employee list`
                menu = saxHandler.getmenu();

            } catch (Exception ex) {
                Log.d("XML", "SAXXMLParser: parse() failed");
            }

            // return Employee list
            return menu;
    }



}

SAXXMLHandler.java

public class SAXXMLHandler extends DefaultHandler {

    private List<Item> menu;
    private String tempVal;
    private Item tempEmp;

    public SAXXMLHandler() {
        menu = new ArrayList<Item>();
    }

    public List<Item> getmenu() {
        return menu;
    }

    // Event Handlers
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        // reset
        tempVal = "";
        if (qName.equalsIgnoreCase("item")) {
            // create a new instance of employee
            tempEmp = new Item();
        }
    }

    public void characters(char[] ch, int start, int length)
            throws SAXException {
        tempVal = new String(ch, start, length);
    }

    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        if (qName.equalsIgnoreCase("item")) {
            // add it to the list
            menu.add(tempEmp);
        } else if (qName.equalsIgnoreCase("id")) {
            tempEmp.setId(Integer.parseInt(tempVal));
        } else if (qName.equalsIgnoreCase("name")) {
            tempEmp.setName(tempVal);
        } else if (qName.equalsIgnoreCase("category")) {
            tempEmp.setcategory(tempVal);
        } else if (qName.equalsIgnoreCase("published")) {
            tempEmp.setpublished(tempVal);
        } 
    }

}

Android manifesto.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.xmlspinner"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.xmlspinner.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

logcat detail

http://i.stack.imgur.com/BpNKV.jpg
هل كانت مفيدة؟

المحلول

I have done changes in codes try below codes...

    import java.net.URL;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")
public class XMLParsingDOMExample extends Activity implements AdapterView.OnItemSelectedListener {

    ArrayList<String> title;

    Button button;
    Spinner spinner;

    ArrayAdapter<String> from_adapter;

    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        title = new ArrayList<String>();


        button = (Button) findViewById(R.id.button1);
        spinner = (Spinner) findViewById(R.id.spinner1);
        spinner.setOnItemSelectedListener(this);

        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }


        button.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                parse();

                from_adapter=new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_spinner_item, title);
                from_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinner.setAdapter(from_adapter);



            }

        });


    }

    public void onItemSelected(AdapterView<?> parent, View view, int pos,
            long id) {
        Toast.makeText(parent.getContext(), ""+spinner.getSelectedItem().toString().trim(),
                Toast.LENGTH_LONG).show();
    }

    public void onNothingSelected(AdapterView<?> arg0) {
    }

    protected void parse() {
        // TODO Auto-generated method stub


        try {

            URL url = new URL(
                    "http://www.androidituts.com/source/tutorials.xml");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new InputSource(url.openStream()));
            doc.getDocumentElement().normalize();

            NodeList nodeList = doc.getElementsByTagName("item");
            for (int i = 0; i < nodeList.getLength(); i++) {

                Node node = nodeList.item(i);       

                Element fstElmnt = (Element) node;
                NodeList nameList = fstElmnt.getElementsByTagName("id");
                Element nameElement = (Element) nameList.item(0);
                nameList = nameElement.getChildNodes();         


                NodeList websiteList = fstElmnt.getElementsByTagName("name");
                Element websiteElement = (Element) websiteList.item(0);
                websiteList = websiteElement.getChildNodes();

                NodeList websiteList1 = fstElmnt.getElementsByTagName("category");
                Element websiteElement1 = (Element) websiteList1.item(0);
                websiteList1 = websiteElement1.getChildNodes();

                NodeList websiteList2 = fstElmnt.getElementsByTagName("published");
                Element websiteElement2 = (Element) websiteList2.item(0);
                websiteList2 = websiteElement2.getChildNodes();


                title.add(((Node) nameList.item(0)).getNodeValue()+":"+((Node) websiteList.item(0)).getNodeValue() +"\n"+((Node) websiteList1.item(0)).getNodeValue()+"-"+((Node) websiteList2.item(0)).getNodeValue());

            }
        } catch (Exception e) {
            System.out.println("XML Pasing Excpetion = " + e);
        }

    }

}

Now you can get all the details in that spinner...

نصائح أخرى

  1. You can't Performing Network Operations ON GUI thread so you have to create a thread to fitch and parse data from URL.

    public void onClick(View v) {
    
    new Thread(new Runnable() {
    
        @Override
        public void run() {
            item = SAXXMLParser.parse(URL);
            spinhandler.sendEmptyMessage(0);
        }
    }).start();
    

    }

  2. Create Handler

    private Handler spinhandler = new Handler() {
        @Override
            public void handleMessage(Message msg) {
                if(msg.what == 0) {
                ArrayAdapter<Item> adapter = new ArrayAdapter<Item>(MainActivity.this, R.layout.list_item, item);
                spinner.setAdapter(adapter);
                spinner.setOnItemSelectedListener(MainActivity.this);
                }
            }
        };
    
  3. When try to put data in Item Object the Item object equal null so you have to intialize the object before start access it

        if (qName.equalsIgnoreCase("id")) {
            tempEmp = new Item();
            tempEmp.setId(Integer.parseInt(tempVal));
        }
    
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top