xmlpullparser解析

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

你可能感兴趣的:(android,xmlpullparser)