解析XML

public static List<FileItem> ParserXML(InputStream is)
			throws IllegalStateException, IOException {
		
		XmlPullParser parser = Xml.newPullParser();
		List<FileItem> Items = null;
		try {
			parser.setInput(is, "UTF-8");
			int eventType = parser.getEventType();

			FileItem currentItem = null;

			while (eventType != XmlPullParser.END_DOCUMENT) {
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					Items = new ArrayList<FileItem>();
					break;
				case XmlPullParser.START_TAG:
					String name = parser.getName();
					if (name.equalsIgnoreCase(fileroot)) {
						currentItem = new FileItem();
					} else if (currentItem != null) {
						if (name.equalsIgnoreCase(currentItem.NAME)) {
							currentItem.setDisplayName(parser.nextText());
						} else if (name.equalsIgnoreCase(currentItem.PATH)) {
							currentItem.setPath(parser.nextText());
						} else if (name.equalsIgnoreCase(currentItem.IS_DIR)) {
							currentItem.setIS_Dir(parser.nextText());
						} else if (name.equalsIgnoreCase(currentItem.SIZE)) {
							currentItem.setSize(parser.nextText());
						} else if (name.equalsIgnoreCase(currentItem.DATE)) {
							currentItem.setDate(parser.nextText());
						}
					}
					break;
				case XmlPullParser.END_TAG:
					if (parser.getName().equalsIgnoreCase(fileroot)
							&& currentItem != null) {
						Items.add(currentItem);
						currentItem = null;
					}
					break;
				}
				eventType = parser.next();
			}
			is.close();
			// ent.consumeContent();
			System.out.println("items size" + Items.size());
			return Items;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

你可能感兴趣的:(xml)