`
wayfarer
  • 浏览: 299101 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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

分享到:
评论

相关推荐

    chm 格式的kxml 帮助文档

    chm格式的kxml doc,用quickchm做的,不过索引没做,因为kxml的类少。

    kxml2_doc_src

    这个压缩包“kxml2_doc_src”包含了KXML2的完整帮助文档和源代码,为开发者提供了详尽的学习和参考资源。 一、KXML2概述 KXML2是由Kevin Conner开发的,它是一个基于事件驱动的XML解析器,专为J2ME和Android平台...

    2023年Android面试整里大全.doc

    * PULL:经常用在 J2ME 对于节点解决比较好,类似 SAX 方式,同样很节省内存,在 J2ME 中我们经常使用的 KXML 库来解析 五、ListView 的优化方案 * 假如自定义适配器,那么在 getView 方法中要考虑方法传进来的...

    J2ME开发中解析XML

    - **KXML**:KXML是一个增量解析器,它逐段解析XML文档,适合处理较大的数据集。这种方式能够有效地管理内存,尤其适用于资源受限的环境。 - **NanoXML**:NanoXML采用一步解析方式,即一次性加载整个文档到内存中并...

    parserDataWithKissXml

    NSArray *items = [doc nodesForXPath:kXML error:nil]; for (DDXMLElement *obj in items) { xmlData *data = [[xmlData alloc] init]; DDXMLElement *aUser = [obj elementForName:KUSER]; if(aUser) ...

    android解析xml源码和ppt.zip

    - `org.kxml2.io.KXmlParser`:Android系统默认使用的XML Pull解析器实现,基于kXML库。 - 源码中,关注`parse()`方法,它是解析过程的入口,`next()`和`eventType`属性用于跟踪解析进度。 4. 使用PPT进行学习: ...

    java实现新建文件夹源码-simple-java-xml-parser:具有XPath的易用性和拉式解析性能的JavaXML解析器。专为在A

    Java中,`org.xmlpull.v1.XmlPullParser`接口及其实现如KXML2库提供了拉式解析的功能。以下是一个简单的拉式解析示例: ```java import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserFactory...

Global site tag (gtag.js) - Google Analytics