通过URL在Android中解析XML

时间:2012-05-18 06:11:47

标签: android xml parsing url

在android XML Parse中我得到PID = 713错误和gralloc_goldfish错误(即)未检测到GPU仿真的仿真器。,我的编码是

xml_pharseActivity.java

package com;
import java.util.ArrayList;
import com.xml_pharse.R;
import android.app.Activity;
//import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;
public class xml_pharseActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//get a reference to the layout
LayoutInflater inflater = getLayoutInflater();
LinearLayout mainLayout = (LinearLayout) inflater.inflate(R.layout.main,null);
try
{
System.out.println("Inside Try_ActivityClass");
//create an instance of the DefaultHandler class 
//**ALTER THIS FOR YOUR CLASS NAME**
DataHandler handler = new DataHandler(getApplicationContext());
//get the string list by calling the public method
ArrayList<TextView> newViews = handler.getData();
//convert to an array
Object[] products = newViews.toArray();
//loop through the items, creating a View item for each
for(int i=0; i<products.length; i++)
{
System.out.println("Inside for_ActivityClass");
//add the next View in the list
mainLayout.addView((TextView)products[i]);
}
System.out.println("Outside for_ActivityClass");
}
catch(Exception pce) {
Log.e("AndroidTestsActivity", "PCE "+pce.getMessage());
System.out.println("Catch1_AcitivityClass");
}
System.out.println("Before MainLayoutSet_AcitivityClass");
setContentView(mainLayout);
System.out.println("After MainLayoutSet_AcitivityClass");
// Intent intent = new Intent(this, DataHandler.class);
// startActivity(intent);
}
}
在datahandler.java中

package com;
import org.xml.sax.helpers.DefaultHandler;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.widget.TextView;
public class DataHandler  extends DefaultHandler {

    //list for imported product data
    private ArrayList<TextView> theViews;
    //string to track each entry
    private String currBrand = "";
    //flag to keep track of XML processing
    private boolean isProduct = false;
    //context for user interface
    private Context theContext;
    //constructor
    public DataHandler(Context cont)
    {
        super();
        theViews = new ArrayList<TextView>();
        theContext = cont;
        System.out.println("Inside DataHandler");
    }
    //start of the XML document
    public void startDocument () 
    { 
        Log.i("DataHandler", "Start of XML document");
        System.out.println("Inside start_Doc"); 
    }

    //end of the XML document
    public void endDocument ()
    { Log.i("DataHandler", "End of XML document"); 
    System.out.println("Inside End_Doc");
    }

    //opening element tag
    public void startElement (String uri, String name, String qName, Attributes atts)
    {
        System.out.println("Inside StartElement");
        //handle the start of an element
        //find out if the element is a brand
        if(qName.equals("brand"))
        {
            System.out.println("Inside StartElement_if");
            //set product tag to false
            isProduct = false;
            //create View item for brand display
            TextView brandView = new TextView(theContext);
            brandView.setTextColor(Color.rgb(73, 136, 83));
            //add the attribute value to the displayed text
            String viewText = "Items from " + atts.getValue("name") + ":";
            brandView.setText(viewText);
            //add the new view to the list
            theViews.add(brandView);
        }
        //the element is a product
        else if(qName.equals("product"))
        {   System.out.println("Inside StartElement_ifelse");
            isProduct = true;
        }
    }
        //closing element tag
    public void endElement (String uri, String name, String qName)
    {
        System.out.println("Inside endElement");
        //handle the end of an element
        if(qName.equals("brand"))
        {
            System.out.println("Inside endElement_if");
            //create a View item for the products
            TextView productView = new TextView(theContext);
            productView.setTextColor(Color.rgb(192, 199, 95));
            //display the compiled items
            productView.setText(currBrand);
            //add to the list
            theViews.add(productView);
            //reset the variable for future items
            currBrand = "";
        }
    }
        //element content
    public void characters (char ch[], int start, int length)
    {
        System.out.println("Inside Characters");
        //process the element content
        //string to store the character content
        String currText = "";
        //loop through the character array
        for (int i=start; i<start+length; i++)
        {
            System.out.println("Inside Characters_For _character array");
            switch (ch[i]) {
            case '\\':
                break;
            case '"':
                break;
            case '\n':
                break;
            case '\r':
                break;
            case '\t':
                break;
            default:
                currText += ch[i];
                break;
            }
        }
        //prepare for the next item
        if(isProduct && currText.length()>0)
        {
            currBrand += currText+"\n";
            System.out.println("Inside Characters_If");
        }
    }
    public ArrayList<TextView> getData() {
        System.out.println("Inside ArrayList<TextView> getData() ");
        // TODO Auto-generated method stub
        //take care of SAX, input and parsing errors
        try
        {
            System.out.println("Inside ArrayList<TextView> getData() _TRY");
                //set the parsing driver
            System.setProperty("org.xml.sax.driver","org.xmlpull.v1.sax2.Driver");
            System.out.println("After set the parsing driver");
                //create a parser
            SAXParserFactory parseFactory = SAXParserFactory.newInstance();
            SAXParser xmlParser = parseFactory.newSAXParser();
            System.out.println("After create a parser");
                //get an XML reader
            XMLReader xmlIn = xmlParser.getXMLReader();
            System.out.println("After XML Reader");
                //instruct the app to use this object as the handler
            xmlIn.setContentHandler(this);
            System.out.println("After instruct the app to use this object as the handler");
                //provide the name and location of the XML file **ALTER THIS FOR YOUR FILE**
            URL xmlURL = new URL("http://bestpropertyworld.com/MobileApp/new.xml");
            System.out.println("After provide the name and location of the XML file");
                //open the connection and get an input stream
            URLConnection xmlConn = xmlURL.openConnection();
            InputStreamReader xmlStream = new InputStreamReader(xmlConn.getInputStream());
            System.out.println("After open the connection and get an input stream");
                //build a buffered reader
            BufferedReader xmlBuff = new BufferedReader(xmlStream);
            System.out.println("After build a buffered reader");
                //parse the data
            xmlIn.parse(new InputSource(xmlBuff));
            System.out.println("After parse the data"); 
        }
                    catch(SAXException se) { 
            Log.e("AndroidTestsActivity", 
                "SAX Error " + se.getMessage()); 
            System.out.println("Catch1");
            }
        catch(IOException ie) {
            Log.e("AndroidTestsActivity", 
                "Input Error " + ie.getMessage());
            System.out.println("Catch2");
            }
        catch(Exception oe) { 
            Log.e("AndroidTestsActivity", 
                "Unspecified Error " + oe.getMessage());
            System.out.println("Catch3");
            }
            //return the parsed product list
        System.out.println("Outside ArrayList<TextView> getData() _TRY");
        return theViews;
    }
    }

清单代码:::::::

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

    <uses-sdk android:minSdkVersion="15" />
      <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="com.xml_pharseActivity"
            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 :::

中的输出
05-18 10:20:54.892: I/System.out(579): Inside Try_ActivityClass
05-18 10:20:55.082: I/System.out(579): Inside DataHandler
05-18 10:20:55.082: I/System.out(579): Inside ArrayList<TextView> getData() 
05-18 10:20:55.082: I/System.out(579): Inside ArrayList<TextView> getData() _TRY
05-18 10:20:55.091: I/System.out(579): After set the parsing driver
05-18 10:20:55.331: I/dalvikvm(579): threadid=3: reacting to signal 3
05-18 10:20:55.441: I/System.out(579): After create a parser
05-18 10:20:55.441: I/System.out(579): After XML Reader
05-18 10:20:55.452: I/System.out(579): After instruct the app to use this object as 
05-18 10:20:55.452: I/System.out(579): After provide the name and location of the XML 
05-18 10:20:55.488: I/dalvikvm(579): Wrote stack traces to '/data/anr/traces.txt'
05-18 10:20:55.656: I/dalvikvm(579): threadid=3: reacting to signal 3
05-18 10:20:55.722: I/dalvikvm(579): Wrote stack traces to '/data/anr/traces.txt'
05-18 10:20:55.758: E/AndroidTestsActivity(579): Unspecified Error null
05-18 10:20:55.758: I/System.out(579): Catch3
05-18 10:20:55.761: I/System.out(579): Outside ArrayList<TextView> getData() _TRY
05-18 10:20:55.761: I/System.out(579): Outside for_ActivityClass
05-18 10:20:55.761: I/System.out(579): Before MainLayoutSet_AcitivityClass
05-18 10:20:55.965: I/System.out(579): After MainLayoutSet_AcitivityClass
05-18 10:20:56.141: I/dalvikvm(579): threadid=3: reacting to signal 3
05-18 10:20:56.161: I/dalvikvm(579): Wrote stack traces to '/data/anr/traces.txt'
05-18 10:21:26.592: I/dalvikvm(713): threadid=3: reacting to signal 3
05-18 10:21:26.718: I/dalvikvm(713): Wrote stack traces to '/data/anr/traces.txt'
05-18 10:21:26.882: I/dalvikvm(713): threadid=3: reacting to signal 3
05-18 10:21:26.935: I/dalvikvm(713): Wrote stack traces to '/data/anr/traces.txt'
05-18 10:21:27.018: I/System.out(713): Inside Try_ActivityClass
05-18 10:21:27.022: I/System.out(713): Inside DataHandler
05-18 10:21:27.022: I/System.out(713): Inside ArrayList<TextView> getData() 
05-18 10:21:27.022: I/System.out(713): Inside ArrayList<TextView> getData() _TRY
05-18 10:21:27.022: I/System.out(713): After set the parsing driver
05-18 10:21:27.062: I/System.out(713): After create a parser
05-18 10:21:27.062: I/System.out(713): After XML Reader
05-18 10:21:27.062: I/System.out(713): After instruct the app to use this object as    the handler
05-18 10:21:27.082: I/System.out(713): After provide the name and location of the XML file
05-18 10:21:27.125: E/AndroidTestsActivity(713): Unspecified Error null
05-18 10:21:27.125: I/System.out(713): Catch3
05-18 10:21:27.125: I/System.out(713): Outside ArrayList<TextView> getData() _TRY
05-18 10:21:27.125: I/System.out(713): Outside for_ActivityClass
05-18 10:21:27.125: I/System.out(713): Before MainLayoutSet_AcitivityClass
05-18 10:21:27.432: I/System.out(713): After MainLayoutSet_AcitivityClass
05-18 10:21:27.432: I/dalvikvm(713): threadid=3: reacting to signal 3
05-18 10:21:27.483: I/dalvikvm(713): Wrote stack traces to '/data/anr/traces.txt'
05-18 10:21:27.972: D/gralloc_goldfish(713): Emulator without GPU emulation detected.
05-18 10:21:28.181: I/dalvikvm(713): threadid=3: reacting to signal 3
05-18 10:21:28.240: I/dalvikvm(713): Wrote stack traces to '/data/anr/traces.txt'
05-18 10:21:28.442: I/dalvikvm(713): threadid=3: reacting to signal 3
05-18 10:21:28.473: I/dalvikvm(713): Wrote stack traces to '/data/anr/traces.txt'

0 个答案:

没有答案