4.KVO,KVO,通知之objective-c
2012-08-16 15:41
232 查看
基于dom方式的dom4j和jdom以及JDK提供的dom方式,都是基于树形结构把xml文本数据读入内存后检索或修改的。
而sax方式是以缓存的方式流读入的方式,检索输入的文本信息。用方式通知实现方法显示数据的。
所以当文件较大时候,就会出现性能和内存溢出(outofMemoryError)了。
以上是本人的自己的理解,仅供参考,具体的信息还请参考官方文档。
<?xml version="1.0" encoding="utf-8"?>
<schoolList>
<school id="10001" name="一中" >1000人</school>
<school id="10002" name="二中" >2000人</school>
<school id="10003" name="三中" >3000人</school>
<school id="10004" name="四中" >4000人</school>
</schoolList>
package com.liuxt.xml.tutorial;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class DomParse {
public static String getTypeName(short value){
switch(value){
case Node.ELEMENT_NODE: return "ELEMENT_NODE";
case Node.ATTRIBUTE_NODE: return "ATTRIBUTE_NODE";
case Node.CDATA_SECTION_NODE: return "CDATA_SECTION_NODE";
case Node.COMMENT_NODE: return "COMMENT_NODE";
case Node.DOCUMENT_FRAGMENT_NODE: return "DOCUMENT_FRAGMENT_NODE";
case Node.DOCUMENT_NODE: return "DOCUMENT_NODE";
case Node.ENTITY_NODE: return "ENTITY_NODE";
case Node.TEXT_NODE: return "TEXT_NODE";
}
return null;
}
public static void main(String arge[]) {
long beginTime = System.currentTimeMillis();
DocumentBuilderFactory factory;
DocumentBuilder builder;
try {
File f = new File("xml/school.xml");
factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
NodeList schoolList = doc.getElementsByTagName("school");
Node schoolNode,idNode,nameNode;
for (int i = 0; i < schoolList.getLength(); i++) {
schoolNode=schoolList.item(i);
showNodeInfo(schoolNode);
idNode=schoolNode.getAttributes().getNamedItem("id");
showNodeInfo(idNode);
nameNode=schoolNode.getAttributes().getNamedItem("id");
showNodeInfo(nameNode);
}
} catch (Exception e) {
e.printStackTrace();
}
showTime(beginTime);
}
private static void showTime(long lasting) {
System.out.println("运行时间:" + (System.currentTimeMillis() - lasting) + " 毫秒");
}
private static void showNodeInfo(Node schoolNode) {
String nodeName;
String nodeValue;
short nodeType;
nodeType=schoolNode.getNodeType();
nodeName=schoolNode.getNodeName();
nodeValue=schoolNode.getNodeValue();
System.out.println("node Type is :"+getTypeName(nodeType));
System.out.println("node name is :"+nodeName);
System.out.println("node value is :"+nodeValue);
}
}
package com.liuxt.xml.tutorial;
import java.util.Iterator;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
public class JdomParse {
public static void main(String[] args) {
try {
Document document = new SAXBuilder().build("xml/school.xml");
Element schools = document.getRootElement();
Iterator it = schools.getChildren().iterator();
while (it.hasNext()) {
Element school = (Element) it.next();
String id=school.getAttributeValue("id");
String name=school.getAttributeValue("name");
System.out.println("id:"+id +" name:"+name);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.liuxt.xml.tutorial;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SaxParse extends DefaultHandler {
java.util.Stack tags = new java.util.Stack();
public SaxParse() {
super();
}
public static void main(String args[]) {
long lasting = System.currentTimeMillis();
try {
SAXParserFactory sf = SAXParserFactory.newInstance();
SAXParser sp = sf.newSAXParser();
DefaultHandler reader = new SaxParse();
sp.parse(new InputSource("xml/school.xml"), reader);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("运行时间:" + (System.currentTimeMillis() - lasting)
+ " 毫秒");
}
public void characters(char ch[], int start, int length)
throws SAXException {
String tag = (String) tags.peek();
if (tag.equals("school")) {
System.out.println("学校人数:" + new String(ch, start, length));
}
}
@SuppressWarnings("unchecked")
public void startElement(String uri, String localName, String qName,
Attributes attrs) {
tags.push(qName);
if(qName.equals("school")){
System.out.println(qName);
System.out.println("id:"+attrs.getValue("id"));
System.out.println("name:"+attrs.getValue("name"));
}
}
}
而sax方式是以缓存的方式流读入的方式,检索输入的文本信息。用方式通知实现方法显示数据的。
所以当文件较大时候,就会出现性能和内存溢出(outofMemoryError)了。
以上是本人的自己的理解,仅供参考,具体的信息还请参考官方文档。
<?xml version="1.0" encoding="utf-8"?>
<schoolList>
<school id="10001" name="一中" >1000人</school>
<school id="10002" name="二中" >2000人</school>
<school id="10003" name="三中" >3000人</school>
<school id="10004" name="四中" >4000人</school>
</schoolList>
package com.liuxt.xml.tutorial;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class DomParse {
public static String getTypeName(short value){
switch(value){
case Node.ELEMENT_NODE: return "ELEMENT_NODE";
case Node.ATTRIBUTE_NODE: return "ATTRIBUTE_NODE";
case Node.CDATA_SECTION_NODE: return "CDATA_SECTION_NODE";
case Node.COMMENT_NODE: return "COMMENT_NODE";
case Node.DOCUMENT_FRAGMENT_NODE: return "DOCUMENT_FRAGMENT_NODE";
case Node.DOCUMENT_NODE: return "DOCUMENT_NODE";
case Node.ENTITY_NODE: return "ENTITY_NODE";
case Node.TEXT_NODE: return "TEXT_NODE";
}
return null;
}
public static void main(String arge[]) {
long beginTime = System.currentTimeMillis();
DocumentBuilderFactory factory;
DocumentBuilder builder;
try {
File f = new File("xml/school.xml");
factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
NodeList schoolList = doc.getElementsByTagName("school");
Node schoolNode,idNode,nameNode;
for (int i = 0; i < schoolList.getLength(); i++) {
schoolNode=schoolList.item(i);
showNodeInfo(schoolNode);
idNode=schoolNode.getAttributes().getNamedItem("id");
showNodeInfo(idNode);
nameNode=schoolNode.getAttributes().getNamedItem("id");
showNodeInfo(nameNode);
}
} catch (Exception e) {
e.printStackTrace();
}
showTime(beginTime);
}
private static void showTime(long lasting) {
System.out.println("运行时间:" + (System.currentTimeMillis() - lasting) + " 毫秒");
}
private static void showNodeInfo(Node schoolNode) {
String nodeName;
String nodeValue;
short nodeType;
nodeType=schoolNode.getNodeType();
nodeName=schoolNode.getNodeName();
nodeValue=schoolNode.getNodeValue();
System.out.println("node Type is :"+getTypeName(nodeType));
System.out.println("node name is :"+nodeName);
System.out.println("node value is :"+nodeValue);
}
}
package com.liuxt.xml.tutorial;
import java.util.Iterator;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
public class JdomParse {
public static void main(String[] args) {
try {
Document document = new SAXBuilder().build("xml/school.xml");
Element schools = document.getRootElement();
Iterator it = schools.getChildren().iterator();
while (it.hasNext()) {
Element school = (Element) it.next();
String id=school.getAttributeValue("id");
String name=school.getAttributeValue("name");
System.out.println("id:"+id +" name:"+name);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.liuxt.xml.tutorial;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SaxParse extends DefaultHandler {
java.util.Stack tags = new java.util.Stack();
public SaxParse() {
super();
}
public static void main(String args[]) {
long lasting = System.currentTimeMillis();
try {
SAXParserFactory sf = SAXParserFactory.newInstance();
SAXParser sp = sf.newSAXParser();
DefaultHandler reader = new SaxParse();
sp.parse(new InputSource("xml/school.xml"), reader);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("运行时间:" + (System.currentTimeMillis() - lasting)
+ " 毫秒");
}
public void characters(char ch[], int start, int length)
throws SAXException {
String tag = (String) tags.peek();
if (tag.equals("school")) {
System.out.println("学校人数:" + new String(ch, start, length));
}
}
@SuppressWarnings("unchecked")
public void startElement(String uri, String localName, String qName,
Attributes attrs) {
tags.push(qName);
if(qName.equals("school")){
System.out.println(qName);
System.out.println("id:"+attrs.getValue("id"));
System.out.println("name:"+attrs.getValue("name"));
}
}
}
相关文章推荐
- 4.KVO,KVO,通知之objective-c
- 4.KVO,KVO,通知之objective-c
- 移动开发(IOS) – Objective-C-10-KVC、谓词、KVO与通知
- [objective-c] 09 - 开发模式 单例 KVO 通知
- Objective-c KVC and KVO and 通知
- objective-c介绍之 KVO,KVO,通知
- 一句代码,更加优雅的调用KVO和通知
- Objective-C_语言_KVO和KVC
- Objective-C语法之KVO的使用
- Objective-C 通知
- 设计模式-01.代理、通知、KVO对比
- Objective-C语法之KVO的使用
- Objective-C语法之KVO的使用
- 『IOS』IOS开发系列--Objective-C之KVC、KVO
- Objective-C中的KVC和KVO
- iOS开发——高级篇——iOS中如何选择delegate、通知、KVO(以及三者的区别)
- Objective-C 【KVO(键值监听)】
- Objective-C KVC - KVO
- 通知机制和KVO机制
- IOS学习笔记--Objective-C之KVC、KVO