标签:main tab package pre document 功能 extend 比较 file
DOM解析 |
SAX解析 |
原理: 一次性加载xml文档,不适合大容量的文件读取 |
原理: 加载一点,读取一点,处理一点。适合大容量文件的读取 |
DOM解析可以任意进行增删改成 |
SAX解析只能读取 |
DOM解析任意读取任何位置的数据,甚至往回读 |
SAX解析只能从上往下,按顺序读取,不能往回读 |
DOM解析面向对象的编程方法(Node,Element,Attribute),Java开发者编码比较简单。 |
SAX解析基于事件的编程方法。java开发编码相对复杂。 |
2 SAX解析工具:
SAX解析工具- Sun公司提供的。内置在jdk中。org.xml.sax.*
核心的API:
SAXParser类: 用于读取和解析xml文件对象
parse(File f, DefaultHandler dh)方法: 解析xml文件
参数一: File:表示 读取的xml文件。
参数二: DefaultHandler: SAX事件处理程序。使用DefaultHandler的子类
例如:{
1.创建SAXParser对象
SAXParser parser=SAXParserFactory.newInstance().newSAXParser();
2.调用parse方法
parser.parse(new File("./src/contact.xml"), new MyDefaultHandler());
}
3.DefaultHandler类的API:
void startDocument() : 在读到文档开始时调用
void endDocument() :在读到文档结束时调用
void startElement(String uri, String localName, String qName, Attributes attributes) :读到开始标 签时调用
void endElement(String uri, String localName, String qName) :读到结束标签时调用
void characters(char[] ch, int start, int length) : 读到文本内容时调用
3.SAM解析练习:
1.基本练习,查看函数调用;
SamExercise.java:
package com.dom4j.SAM; import java.io.File; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.SAXException; public class SamExercise { public static void main(String[] args) throws Exception, SAXException { SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); parser.parse(new File("./src/contact.xml"), new MyDefalutHandler()); } }
MyDefalutHandler.java:
package com.dom4j.SAM; import javax.xml.stream.events.StartDocument; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class MyDefalutHandler extends DefaultHandler{ @Override public void startDocument() throws SAXException { System.out.println("MyDefalutHandler.startDocument()"); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { System.out.println("MyDefalutHandler.startElement()->>>" + qName); } @Override public void characters(char[] ch, int start, int length) throws SAXException { System.out.println("MyDefalutHandler.characters()->>>" + new String(ch, start, length)); } @Override public void endElement(String uri, String localName, String qName) throws SAXException { System.out.println("MyDefalutHandler.endElement()->>>" + qName); } @Override public void endDocument() throws SAXException { System.out.println("MyDefalutHandler.endDocument()"); } }
2.读取打印出来xml文件:
contact.xml:
<?xml version="1.0" encoding="gb2312"?> <contactList> <contact id="001"> <name>张三</name> <age>20</age> <phone>134222223333</phone> <email>zhangsan@qq.com</email> <qq>432221111</qq> </contact> <contact id="003" value="hello"> <name>lisi</name> <age>20</age> <phone>134222225555</phone> <email>lisi@qq.com</email> <qq>432222222</qq> </contact> <contact></contact> </contactList>
DisplayAllContent.java:
package com.dom4j.SAM; import java.io.File; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.SAXException; public class DisplayAllContent { public static void main(String[] args) throws Exception, SAXException { SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); MyDisplayAllContentHandle handle = new MyDisplayAllContentHandle(); parser.parse(new File("./src/contact.xml"), handle); System.out.println(handle.getContent()); } }
MyDisplayAllContentHandle:
package com.dom4j.SAM; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class MyDisplayAllContentHandle extends DefaultHandler { private static StringBuffer sb; static{ sb = new StringBuffer(); } public String getContent(){ return sb.toString(); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { sb.append("<"); sb.append(qName); for(int i = 0; i < attributes.getLength(); i++){ sb.append(" " + attributes.getQName(i)); sb.append("=" + "\"" + attributes.getValue(i) + "\""); } sb.append(">"); } @Override public void characters(char[] ch, int start, int length) throws SAXException { sb.append(new String(ch, start, length)); } @Override public void endElement(String uri, String localName, String qName) throws SAXException { sb.append("</"); sb.append(qName + ">"); } }
3.将xml文件内容放在一个类中,并输出:
contact.java:
package com.dom4j.SAM; public class contact { private String id; private String name; private String age; private String qq; private String email; private String phone; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getqq() { return qq; } public void setqq(String qq) { this.qq = qq; } public String getemail() { return email; } public void setEmail(String email) { this.email = email; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } @Override public String toString() { return "Person \n id=" + id + ", name=" + name + ", age=" + age + ", qq=" + qq + ", email=" + email + ", phone=" + phone + "\n"; } }
ReadValue.java:
package com.dom4j.SAM; import java.io.File; import java.util.ArrayList; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.SAXException; public class ReadValue{ public static void main(String[] args) throws Exception, SAXException { SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); MyReadValueHandle handle = new MyReadValueHandle(); parser.parse(new File("./src/contact.xml"), handle); ArrayList<contact> list = handle.getList(); for(contact acontact : list){ System.out.println(acontact); } } }
MyReadValueHandle.java:
package com.dom4j.SAM; import java.util.ArrayList; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class MyReadValueHandle extends DefaultHandler { private ArrayList<contact> list = new ArrayList<contact>(); private contact acontact; private String curTag; public ArrayList<contact> getList(){ return list; } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { curTag = qName; if(qName.equals("contact")){ acontact = new contact(); acontact.setId(attributes.getValue("id")); } } @Override public void characters(char[] ch, int start, int length) throws SAXException { String content = new String(ch, start, length); if("name".equals(curTag)){ acontact.setName(content); } if("age".equals(curTag)){ acontact.setAge(content); } if("phone".equals(curTag)){ acontact.setPhone(content); } if("email".equals(curTag)){ acontact.setEmail(content); } if("qq".equals(curTag)){ acontact.setqq(content); } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { curTag = null;//注意!!避免换行空格的影响 if(qName.equals("contact")){ list.add(acontact); } } }
4.设计一个通讯录程序
联系人: 编号 姓名 性别 年龄 电话 QQ 邮箱
功能要求:
添加联系人
修改联系人
删除联系人
查询所有联系人
标签:main tab package pre document 功能 extend 比较 file
原文地址:http://www.cnblogs.com/handsomecui/p/6036559.html