使用 Android 解析 XML

发布于 2024-12-08 11:10:53 字数 1302 浏览 0 评论 0原文

我正在开发一个 Android 2.2 应用程序。

我在 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)
    {
...
    }
}

你能帮我吗?

I'm developing an Android 2.2 application.

I have the following XML file on res/xml:

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

I'm getting crazy because I can't find an example to see how can I parse this file.

I also have the following class to store data retrieved:

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

This my method to parse it (it's incompleted):

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)
    {
...
    }
}

Can you help me please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七度光 2024-12-15 11:10:53

您可以尝试这个示例:

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 中提取的信息

You can try this example:

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

}

The Android UI XML file would be:

<?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>

And the XML parsed in this example is this one:

<?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>

NOTE: Info extracted from Android-er

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文