2011-10-05 160 views
1

我正在開發一個Android 2.2應用程序。用Android解析XML

我有RES/XML下面的XML文件:

<?xml version="1.0" encoding="iso-8859-1"?> 
<data> 
    <turo> 
     <name>Rodo</name> 
     <latitude>37.212123</latitude> 
     <longitude>0.1231231</longitude> 
    </turo> 
</data> 

我越來越瘋狂,因爲我無法找到一個例子,看我怎麼能解析該文件。

我也有下面的類來存儲檢索的數據:

public class Turo{ 
    private String name; 
    private Location location; 

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

    public void setLocation(Location location) { 
     this.location = location; 
    } 
    public Location getLocation() { 
     return location; 
    } 
} 

這是我的方法來解析它(它未完):

public Vector<Turo> getTurosFromXML(Activity activity, int xmlId) throws XmlPullParserException, IOException { 
    StringBuffer stringBuffer = new StringBuffer(); 
    Resources res = activity.getResources(); 

    XmlResourceParser xpp = res.getXml(xmlId); 
    xpp.next(); 
    int eventType = xpp.getEventType(); 

    while (eventType != XmlPullParser.END_DOCUMENT) 
    { 
... 
    } 
} 

你能幫助我嗎?

回答

0

你可以試試這個例子:

package com.exercise.AndroidXmlResource; 

import java.io.IOException; 

import org.xmlpull.v1.XmlPullParser; 
import org.xmlpull.v1.XmlPullParserException; 

import android.app.Activity; 
import android.content.res.Resources; 
import android.content.res.XmlResourceParser; 
import android.os.Bundle; 
import android.widget.TextView; 

public class AndroidXmlResource extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    TextView myXmlContent = (TextView)findViewById(R.id.my_xml); 
    String stringXmlContent; 
    try { 
     stringXmlContent = getEventsFromAnXML(this); 
     myXmlContent.setText(stringXmlContent); 
    } catch (XmlPullParserException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

private String getEventsFromAnXML(Activity activity) throws XmlPullParserException, IOException { 
    StringBuffer stringBuffer = new StringBuffer(); 
    Resources res = activity.getResources(); 
    XmlResourceParser xpp = res.getXml(R.xml.myxml); 
    xpp.next(); 
    int eventType = xpp.getEventType(); 
    while (eventType != XmlPullParser.END_DOCUMENT) { 
     if(eventType == XmlPullParser.START_DOCUMENT) { 
       stringBuffer.append("--- Start XML ---"); 
     } else if(eventType == XmlPullParser.START_TAG) { 
      stringBuffer.append("\nSTART_TAG: "+xpp.getName()); 
     } else if(eventType == XmlPullParser.END_TAG) { 
      stringBuffer.append("\nEND_TAG: "+xpp.getName()); 
     } else if(eventType == XmlPullParser.TEXT) { 
      stringBuffer.append("\nTEXT: "+xpp.getText()); 
     } 
     eventType = xpp.next(); 
    } 
    stringBuffer.append("\n--- End XML ---"); 
    return stringBuffer.toString(); 
} 

}

Android的UI XML文件將是:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" /> 
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/my_xml" /> 
</LinearLayout> 

而且在這個例子中解析的XML是這一個:

<?xml version="1.0" encoding="utf-8"?> 
<rootelement1> 
    <subelement> 
     Hello XML Sub-Element 1 
    </subelement> 
    <subelement> 
     Hello XML Sub-Element 2 
     <subsubelement>Sub Sub Element</subsubelement> 
    </subelement> 
</rootelement1> 

注意:信息摘自Android-er