0

我一直在尝试使用 XmlResourceParser,但我觉得它不是适合这项工作的工具。我有一组包含子项目的项目,我想提取一个特定项目,例如此列表中的第二个项目:

<story>
    <id>1</id>
    <name>The First Room</name>
    <description>You aren't sure how you ended up here, but there is nothing in this room of interest. You should probably escape.</description>
    <direction>
        <name>North</name>
        <id>2</id>
    </direction>
    <direction>
        <name>East</name>
        <id>3</id>
    </direction>
</story>
<story>
    <id>2</id>
    <name>Moldy Room</name>
    <description>This room is filled with mold. It would be hazardous to your heath to stick around here.</description>
    <direction>
        <name>South</name>
        <id>1</id>
    </direction>
    <direction>
        <name>West</name>
        <id>4</id>
    </direction>
</story>

简而言之,我希望能够通过“id”数字拉动它们,而不必设置我自己的对象。如果可能的话。

4

1 回答 1

0

您可以使用此代码:

        Resources res = activity.getResources();
        XmlResourceParser xpp = res
                .getXml(R.xml.myxml);
        xpp.next();
        int eventType = xpp.getEventType();
        eventType = xpp.next();
        eventType = xpp.next();
        eventType = xpp.next();

        short id = Short
                .parseShort(xpp.getText());

        Toast.makeText(
                activity.getApplicationContext(),
                "" + id, Toast.LENGTH_LONG)
                .show();
于 2012-05-29T17:57:39.627 回答