【问题标题】:Simple xml SAX parser error while retrieve from HTTP从 HTTP 检索时出现简单的 xml SAX 解析器错误
【发布时间】:2013-08-20 07:44:19
【问题描述】:

实际上,我尝试使用 SAX 解析器解析包含一些细节的简单 XML,并在 Spinner 中显示结果。但我无法确定我的问题中的问题是什么,我的意思是可能是语法错误或我的程序不正确。谁能帮我解决这个问题。

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 宣言。

<?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 详细信息

http://i.stack.imgur.com/BpNKV.jpg

【问题讨论】:

    标签: android xml-parsing android-spinner saxparser


    【解决方案1】:

    我已经修改了代码试试下面的代码...

        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);
            }
    
        }
    
    }
    

    现在您可以获取该微调器中的所有详细信息...

    【讨论】:

      【解决方案2】:
      1. 您无法在 GUI 线程上执行网络操作,因此您必须创建一个线程来匹配和解析来自 URL 的数据。

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

        }

      2. 创建处理程序

        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. 当尝试将数据放入 Item 对象时,Item 对象等于 null,因此您必须在开始访问之前初始化该对象

            if (qName.equalsIgnoreCase("id")) {
                tempEmp = new Item();
                tempEmp.setId(Integer.parseInt(tempVal));
            }
        

      【讨论】:

      • 我在哪里可以使用线程......我需要在 main_activity 中做的 3 项更改?
      • 前两个更改必须添加到 MainActivity.java 最后一个添加到 SAXXMLHandler.java
      • @Androidlearner 如果这篇文章对您有帮助,请将答案标记为正确
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-15
      相关资源
      最近更新 更多