试图解析XML文件

时间:2015-07-14 06:56:49

标签: java xml parsing

import android.app.Activity;
import android.util.Log;
import android.view.MotionEvent;

public class SlidableActivity extends Activity {
    protected static final String TAG = "SlidableActivity";
    private static final int ACTION_TYPE_DEFAULT = 0;
    private static final int ACTION_TYPE_UP = 1;
    private static final int ACTION_TYPE_RIGHT = 2;
    private static final int ACTION_TYPE_DOWN = 3;
    private static final int ACTION_TYPE_LEFT = 4;
    private static final int SLIDE_RANGE = 100;
    private float mTouchStartPointX;
    private float mTouchStartPointY;
    private int mActionType = ACTION_TYPE_DEFAULT;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getRawX();
        int y = (int) event.getRawY();
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mTouchStartPointX = event.getRawX();
            mTouchStartPointY = event.getRawY();
            break;
        case MotionEvent.ACTION_MOVE:
            if (mTouchStartPointX - x > SLIDE_RANGE) {
                mActionType = ACTION_TYPE_LEFT;
            } else if (x - mTouchStartPointX > SLIDE_RANGE) {
                mActionType = ACTION_TYPE_RIGHT;
            } else if (mTouchStartPointY - y > SLIDE_RANGE) {
                mActionType = ACTION_TYPE_UP;
            } else if (y - mTouchStartPointY > SLIDE_RANGE) {
                mActionType = ACTION_TYPE_DOWN;
            }
            break;
        case MotionEvent.ACTION_UP:
            if (mActionType == ACTION_TYPE_UP) {
                slideUp();
            } else if (mActionType == ACTION_TYPE_RIGHT) {
                slideToRight();
            } else if (mActionType == ACTION_TYPE_DOWN) {
                slideDown();
            } else if (mActionType == ACTION_TYPE_LEFT) {
                slideToLeft();
            }
            break;
        default:
            break;
        }
        return true;
    }

    protected void slideToLeft() {
        Log.d(TAG, "slideToLeft() was called.");
    }

    protected void slideToRight() {
        Log.d(TAG, "slideToRight() was called.");
    }

    protected void slideUp() {
        Log.d(TAG, "slideUp() was called.");
    }

    protected void slideDown() {
        Log.d(TAG, "slideDown() was called.");
    }
}

我正在尝试检索 dataItemLabel refDataItem 。 我可以检索它但它来了3次因为我正在检索 listColumns 的nodeList,如下所示:

    <list name="List2" refQuery="Query1" showColumnTitles="hide">
                                                <listColumns>
                                                    <listColumn>
                                                        <listColumnTitle>
                                                            <contents/>
                                                        </listColumnTitle>
                                                        <listColumnBody>
                                                            <contents>
                                                                <list name="List3" refQuery="Query1" showColumnTitles="hide">
                                                                    <listColumns>
                                                                        <listColumn>
                                                                            <listColumnTitle>
                                                                                <contents/>
                                                                            </listColumnTitle>
                                                                            <listColumnBody>
                                                                                <contents>
                                                                                    <list name="List4" refQuery="Query1">
                                                                                        <style>
                                                                                            <CSS value="border-collapse:collapse"/>
                                                                                            <defaultStyles>
                                                                                                <defaultStyle refStyle="ls"/>
                                                                                            </defaultStyles>
                                                                                        </style>
                                                                                        <listColumns>
                                                                                            <listColumn>
                                                                                                <listColumnTitle>
                                                                                                    <contents>
                                                                                                        <textItem>
                                                                                                            <dataSource>
                                                                                                                <dataItemLabel refDataItem="Product type1"/>
                                                                                                            </dataSource>
                                                                                                        </textItem>
                                                                                                    </contents>
                                                                                                    <style>
                                                                                                        <defaultStyles>
                                                                                                            <defaultStyle refStyle="lt"/>
                                                                                                        </defaultStyles>
                                                                                                    </style>
                                                                                                </listColumnTitle>
                                                                                                <listColumnBody>
                                                                                                    <contents>
                                                                                                        <textItem>
                                                                                                            <dataSource>
                                                                                                                <dataItemValue refDataItem="Product type1"/>
                                                                                                            </dataSource>
                                                                                                        </textItem>
                                                                                                    </contents>
                                                                                                    <style>
                                                                                                        <defaultStyles>
                                                                                                            <defaultStyle refStyle="lc"/>
                                                                                                        </defaultStyles>
                                                                                                    </style>
                                                                                                </listColumnBody>
                                                                                            </listColumn>
                                                                                            <listColumn>
                                                                                                <listColumnTitle>
                                                                                                    <contents>
                                                                                                        <textItem>
                                                                                                            <dataSource>
                                                                                                                <dataItemLabel refDataItem="Product name"/>
                                                                                                            </dataSource>
                                                                                                        </textItem>
                                                                                                    </contents>
                                                                                                    <style>
                                                                                                        <defaultStyles>
                                                                                                            <defaultStyle refStyle="lt"/>
                                                                                                        </defaultStyles>
                                                                                                    </style>
                                                                                                </listColumnTitle>
                                                                                                <listColumnBody>
                                                                                                    <contents>
                                                                                                        <textItem>
                                                                                                            <dataSource>
                                                                                                                <dataItemValue refDataItem="Product name"/>
                                                                                                            </dataSource>
                                                                                                        </textItem>
                                                                                                    </contents>
                                                                                                    <style>
                                                                                                        <defaultStyles>
                                                                                                            <defaultStyle refStyle="lc"/>
                                                                                                        </defaultStyles>
                                                                                                    </style>
                                                                                                </listColumnBody>
                                                                                            </listColumn>
                                                                                            <listColumn>
                                                                                                <listColumnTitle>
                                                                                                    <contents>
                                                                                                        <textItem>
                                                                                                            <dataSource>
                                                                                                                <dataItemLabel refDataItem="Margin"/>
                                                                                                            </dataSource>
                                                                                                        </textItem>
                                                                                                    </contents>
                                                                                                    <style>
                                                                                                        <defaultStyles>
                                                                                                            <defaultStyle refStyle="lt"/>
                                                                                                        </defaultStyles>
                                                                                                    </style>
                                                                                                </listColumnTitle>
                                                                                                <listColumnBody>
                                                                                                    <contents>
                                                                                                        <textItem>
                                                                                                            <dataSource>
                                                                                                                <dataItemValue refDataItem="Margin"/>
                                                                                                            </dataSource>
                                                                                                        </textItem>
                                                                                                    </contents>
                                                                                                    <style>
                                                                                                        <defaultStyles>
                                                                                                            <defaultStyle refStyle="lm"/>
                                                                                                        </defaultStyles>
                                                                                                    </style>
                                                                                                </listColumnBody>
                                                                                            </listColumn>
                                                                                        </listColumns>
                                                                                    </list>
                                                                                </contents>
                                                                                <style>
                                                                                    <defaultStyles>
                                                                                        <defaultStyle refStyle="sb"/>
                                                                                    </defaultStyles>
                                                                                </style>
                                                                            </listColumnBody>
                                                                        </listColumn>
                                                                    </listColumns>
                                                                    <style>
                                                                        <CSS value="border-collapse:collapse;width:100%"/>
                                                                        <defaultStyles>
                                                                            <defaultStyle refStyle="sc"/>
                                                                        </defaultStyles>
                                                                    </style>
</list>

问题是它检索了3次 listColumns ,因此显示 dataItemLabel 的refDataItem 3次,但我需要它只有一次。

任何人都可以帮我解决一次吗?

1 个答案:

答案 0 :(得分:0)

nodeList [0] 是数组 nodeList 中的第一个元素。要引用数组的第一个元素,请使用:

getListColumnsNodes(nodeList[0]);

另一种方法是使用唯一的元素id:

<listColumns id="foo">
nodeList = document.getElementById("foo");

以下是有关使用数组和获取元素的一些信息: