kxml doc

1. KXmlParser

java.lang.Object
  org.kxml2.io.KXmlParser

//定义一个事件采用回调的方式,直到读取xml完毕。
public int getEventType() throws org.xmlpull.v1.XmlPullParserException

//遍历下一个事件,返回一个事件的类型
public int next() throws org.xmlpull.v1.XmlPullParserException, java.io.IOException

//得到当前Tag名字
public String getName()

//获取文本
public String getText()

//得到当前Tag下面的属性数量
public int getAttributeCount()

//得到当前Tag下面指定位置的属性名称
public String getAttributeName(int index)

//得到当前Tag下面指定位置的属性植
public String getAttributeValue(int index)
public String getAttributeValue(java.lang.String namespace, java.lang.String name)

(1)
XmlPullParser.START_DOCUMENT = 0: Signalize that parser is at the very beginning of the document and nothing was read yet. This event type can only be observed by calling getEvent() before the first call to next(), nextToken, or nextTag()).
XmlPullParser.END_DOCUMENT = 1: Logical end of the xml document. Returned from getEventType, next() and nextToken() when the end of the input document has been reached.
Note : calling again next() or nextToken() will result in exception being thrown.
(2)
XmlPullParser.START_TAG = 2: Returned from getEventType(), next(), nextToken() when a start tag was read. The name of start tag is available from getName(), its namespace and prefix are available from getNamespace() and getPrefix() if namespaces are enabled. See getAttribute* methods to retrieve element attributes. See getNamespace* methods to retrieve newly declared namespaces.
XmlPullParser.END_TAG = 3: Returned from getEventType(), next(), or nextToken() when an end tag was read. The name of start tag is available from getName(), its namespace and prefix are available from getNamespace() and getPrefix().
XmlPullParser.TEXT = 4: Character data was read and will is available by calling getText().
Note : next() will accumulate multiple events into one TEXT event, skipping IGNORABLE_WHITESPACE, PROCESSING_INSTRUCTION and COMMENT events, In contrast, nextToken() will stop reading text when any other event is observed. Also, when the state was reached by calling next(), the text value will be normalized, whereas getText() will return unnormalized content in the case of nextToken(). This allows an exact roundtrip without chnanging line ends when examining low level events, whereas for high level applications the text is normalized apropriately.

 

2. 例子

public Object getBasicInfo(String url, ActionDispatcher dispatcher) {
	InputStream is = ActionConstants.getHttpPool().process(url, dispatcher);
	int count = 0, _count = -1;
	String temp = null;
	Vector tags = new Vector();
	try {
		KXmlParser parser = new KXmlParser();
		parser.setInput(new InputStreamReader(is));
		parser.setInput(is, "UTF-8"); //纠正中文乱码
		int eventType = parser.getEventType();
		while (eventType != XmlPullParser.END_DOCUMENT) {
			if (eventType == XmlPullParser.START_TAG) {
				if ("result".equals(parser.getName())) {
					if (parser.getAttributeValue(0) != null) temp = parser.getAttributeValue(0);
					if (!"100".equals(temp)) NaviException.getConnException(temp);
				} else if ("hot_spot".equals(parser.getName())) {
					int size = parser.getAttributeCount();
					for (int k = 0; k < size; k++) {
						if ("name".equals(parser.getAttributeName(k))) {
							if (parser.getAttributeValue(k) != null) temp = parser.getAttributeValue(k);
							if (temp == null || temp.trim().length() < 1) {
							} else {
							}
						} else if ("category_id".equals(parser.getAttributeName(k))) {
						} else if ("id".equals(parser.getAttributeName(k))) {
						}
					}
				} else if ("address".equals(parser.getName())) {
					count = 1;
				} else if ("phone_number".equals(parser.getName())) {
					count = 2;
				} else if ("introduction".equals(parser.getName())) {
					count = 3;
				} else if ("tag".equals(parser.getName())) {
					count = 4;
					_count++;
				}
			} else if (eventType == XmlPullParser.TEXT) {
				if (parser.getText() != null) temp = parser.getText();
				switch (count) {
				case 1:
					count = 0; // 将count清零, 防止将脏数据再次赋值
					if (temp != null && temp.trim().length() >= 1) {
					}
					break;
				case 2:
					count = 0; // 将count清零
					if (temp != null && temp.trim().length() >= 1) {
					}
					break;
				case 3:
					count = 0; // 将count清零
					if (temp != null && temp.trim().length() >= 1) {
					}
					break;
				case 4:
					count = 0; // 将count清零
					if (temp != null && temp.trim().length() >= 1) {
						tags.insertElementAt(temp, _count);
					}
					break;
				}
			}
			eventType = parser.next();
		}
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		if (is != null) {
			try {
				is.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			is = null;
		}
	}
	return null;
}

 

3. 资料下载

http://download.csdn.net/source/1686003

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值