xmlpullparser解析

原创
2013/10/30 10:53
阅读数 146
private ArrayList<PayWall> mPayWalls;
private SoftPower mSoftPower;

public void parse(File fileToParse) throws XmlPullParserException, IOException {
    final XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(true);
    final XmlPullParser xpp = factory.newPullParser();
    final FileInputStream fileInputStream = new FileInputStream(fileToParse);
    xpp.setInput(fileInputStream, "utf-8");

    int eventType = xpp.getEventType();

    ArrayList<PayWall> payWalls = null;
    PayWall payWall = null;
    SoftPower softPower = null;

    while (eventType != XmlPullParser.END_DOCUMENT) {
        if(eventType == XmlPullParser.START_TAG) {
            final String startTagName = xpp.getName();
            if (startTagName.equalsIgnoreCase(XmlConstants.TAG_PARAMETERS)) {
            } else if (startTagName.equalsIgnoreCase(XmlConstants.TAG_PAY_WALLS)) {
                payWalls = new ArrayList<PayWall>();
            } else if (startTagName.equalsIgnoreCase(XmlConstants.PayWall.TAG)) {
                payWall = PayWall.parse(xpp);
            } else if (startTagName.equalsIgnoreCase(XmlConstants.SoftPower.TAG)) {
                softPower = SoftPower.parse(xpp);
            }
        } else if(eventType == XmlPullParser.END_TAG) {
            final String endTagName = xpp.getName();
            if (endTagName.equalsIgnoreCase(XmlConstants.TAG_PAY_WALLS)) {
                mPayWalls = payWalls;
            } else if (endTagName.equalsIgnoreCase(XmlConstants.PayWall.TAG)) {
                payWalls.add(payWall);
            } else if (endTagName.equalsIgnoreCase(XmlConstants.SoftPower.TAG)) {
                mSoftPower = softPower;
            }
        }
        eventType = xpp.next();
    }
}

public List<PayWall> getPayWalls() {
    return mPayWalls;
}

public SoftPower getSoftPower() {
    return mSoftPower;
}
展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部