标签:
一、SAX生成和解析XML文档
1、简介
为解决DOM的问题,出现了SAX。 SAX ,事件驱动。当解析器发现元素开始、元素结束、文本、文档的开始或结束等时,发送事件,程序员编写响应这些事件的代码,保存数据。
优点:不用事先调入整个文档,占用资源少;SAX解析器代码比DOM解析器代码小,适于Applet,下载。
缺点:不是持久的;事件过后,若没保存数据,那么数据就丢了; 无状态性;从事件中只能得到文本,但不知该文本属于哪个元素;
使用场合:Applet;只需XML文档的少量内容,很少回头访问;机器内存少;
2、SAX解析XML步骤
(1)建立接口XmlDocument,规定解析类需要实现的功能;
代码:
package com.xml;
import java.util.List;
import com.xml.domain.Stuff;
public interface XmlDocument {
/**
* 建立XML文档
* @param fileName 文件全路径名称
*/
public void createXml(String fileName,List<Stuff> stuffList);
public void createXml(String fileName);
/**
* 解析XML文档
* @param fileName 文件全路径名称
*/
public void parserXml(String fileName);
}
(2)编写实现类SaxDemo
代码:
package com.xml;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.NodeList;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import org.xml.sax.helpers.DefaultHandler;
import com.xml.domain.Employee;
import com.xml.domain.Manager;
import com.xml.domain.Stuff;
/**
*
* @2.SAX生成和解析XML文档
*/
public class SaxDemo implements XmlDocument {
final static String fileNmae = "F:\\java\\workspace\\MyTest\\src\\com\\xml\\myXML.xml";
public static void main(String args[]) {
SaxDemo sax = new SaxDemo();
List<Stuff> stuffList = new ArrayList<Stuff>();
stuffList.add(new Manager("cxli", "male", 23, "10k", "hight"));
stuffList.add(new Manager("randy", "male", 23, "10k", "middle"));
stuffList.add(new Employee("fengzi", "male", 23, "6k"));
stuffList.add(new Employee("saoxing", "male", 23, "6k"));
sax.createXml(fileNmae, stuffList);
sax.parserXml(fileNmae);
}
//创建XML
public void createXml(String fileName, List<Stuff> stuffList) {
Result resultXml = null;
File xmlFile = new File(fileName);
if (xmlFile.exists()) {
xmlFile.delete();
}
try {
resultXml = new StreamResult(new FileOutputStream(fileName));
SAXTransformerFactory sff = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler th = sff.newTransformerHandler();
th.setResult(resultXml);
Transformer transformer = th.getTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // 编码格式是UTF-8
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // 换行
AttributesImpl attr = new AttributesImpl();
th.startDocument();// 文档开始
th.startElement("", "", "stuffs", attr);
for (Stuff stuff : stuffList) {
if (stuff instanceof Manager) {
th.startElement("", "", "managers", attr);
writeElement(th, attr, stuff);
th.endElement("", "", "managers");
} else if (stuff instanceof Employee) {
th.startElement("", "", "employees", attr);
writeElement(th, attr, stuff);
th.endElement("", "", "employees");
}
}
th.endElement("", "", "stuffs");
th.endDocument(); // 文档结束
} catch (Exception e) {
e.printStackTrace();
}
}
//解析XML
public void parserXml(String fileName) {
SAXParserFactory saxfac = SAXParserFactory.newInstance();
try {
SAXParser saxparser = saxfac.newSAXParser();
InputStream is = new FileInputStream(fileName);
saxparser.parse(is, new MySAXHandler());
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void createXml(String fileName) {
}
public void writeElement(TransformerHandler th, AttributesImpl attr,Stuff stuff) throws SAXException {
if (stuff instanceof Manager) {
Manager manager = (Manager) stuff;
th.startElement("", "", "manager", attr);
th.startElement("", "", "name", attr);
th.characters(manager.getName().toCharArray(), 0, manager.getName().length());
th.endElement("", "", "name");
th.startElement("", "", "sex", attr);
th.characters(manager.getSex().toCharArray(), 0, manager.getSex().length());
th.endElement("", "", "sex");
th.startElement("", "", "age", attr);
th.characters(((manager.getAge()) + "").toCharArray(), 0, ((manager.getAge()) + "").length());
th.endElement("", "", "age");
th.startElement("", "", "salery", attr);
th.characters(manager.getSalery().toCharArray(), 0, manager.getSalery().length());
th.endElement("", "", "salery");
th.startElement("", "", "priority", attr);
th.characters(manager.getPriority().toCharArray(), 0, manager.getPriority().length());
th.endElement("", "", "priority");
th.endElement("", "", "manager");
} else if (stuff instanceof Employee) {
Employee employee = (Employee) stuff;
th.startElement("", "", "employee", attr);
th.startElement("", "", "name", attr);
th.characters(employee.getName().toCharArray(), 0, employee.getName().length());
th.endElement("", "", "name");
th.startElement("", "", "sex", attr);
th.characters(employee.getSex().toCharArray(), 0, "male".length());
th.endElement("", "", "sex");
th.startElement("", "", "age", attr);
th.characters(((employee.getAge()) + "").toCharArray(), 0,((employee.getAge()) + "").length());
th.endElement("", "", "age");
th.startElement("", "", "salery", attr);
th.characters(employee.getSalery().toCharArray(), 0, employee.getSalery().length());
th.endElement("", "", "salery");
th.endElement("", "", "employee");
}
}
public void dealWithNode(String fileName, String nodeName) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
org.w3c.dom.Document document = null;
try {
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.newDocument();
document = builder.parse(new File(fileName));
} catch (Exception e) {
e.printStackTrace();
}
NodeList links = document.getElementsByTagName(nodeName);
for (int i = 1; i < links.getLength() - 1; i++) {
}
}
}
class MySAXHandler extends DefaultHandler {
public boolean hasAttribute = false;
public Attributes attributes = null;
public FileOutputStream fos = null;
public StringBuilder sb = null;
File myXMLFile = null;
public void startDocument() throws SAXException {
System.out.println("文档开始打印了");
}
public void endDocument() throws SAXException {
System.out.println("文档打印结束了");
}
public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {
if (qName.equals("employees")) {
myXMLFile = new File(
"F:\\java\\workspace\\MyTest\\src\\com\\xml\\employees.xml");
sb = new StringBuilder();
sb.append("<?xml version=‘1.0‘ encoding=‘UTF-8‘?>\n");
} else if (qName.equals("managers")) {
myXMLFile = new File(
"F:\\java\\workspace\\MyTest\\src\\com\\xml\\managers.xml");
sb = new StringBuilder();
sb.append("<?xml version=‘1.0‘ encoding=‘UTF-8‘?>\n");
}
dealWithStartElement(qName);
if (attributes.getLength() > 0) {
this.attributes = attributes;
this.hasAttribute = true;
}
}
public void endElement(String uri, String localName, String qName)throws SAXException {
dealWithEndElement(qName);
if (qName.equals("employees") || qName.equals("managers")) {
try {
fos = new FileOutputStream(myXMLFile);
fos.write(sb.toString().getBytes());
} catch (Exception e1) {
e1.printStackTrace();
}
}
if (hasAttribute && (attributes != null)) {
for (int i = 0; i < attributes.getLength(); i++) {
System.out.print(attributes.getValue(0));
}
}
}
public void characters(char[] ch, int start, int length)throws SAXException {
System.out.print(new String(ch, start, length));
String character = new String(ch, start, length);
if (!(character.equals("\n"))) {
sb.append((new String(ch, start, length)));
}
}
public void dealWithStartElement(String qName) {
if ((qName.equals("employees")) || (qName.equals("managers"))|| (qName.equals("employee")) || (qName.equals("manager"))) {
System.out.print("<" + qName + ">");
sb.append("<" + qName + ">" + "\n");
}
if ((qName.equals("name")) || (qName.equals("sex"))|| (qName.equals("age")) || (qName.equals("salery"))|| (qName.equals("priority"))) {
System.out.print("<" + qName + ">");
sb.append("<" + qName + ">");
}
}
public void dealWithEndElement(String qName) {
if ((qName.equals("employees")) || (qName.equals("managers"))
|| (qName.equals("employee")) || (qName.equals("manager"))
|| (qName.equals("name")) || (qName.equals("sex"))
|| (qName.equals("age")) || (qName.equals("salery"))
|| (qName.equals("priority"))) {
System.out.print("</" + qName + ">");
sb.append("</" + qName + ">" + "\n");
}
}
}
(3)相关前期准备工作
最后生成的XML如下:
<?xml version="1.0" encoding="UTF-8"?>
<stuffs>
<managers>
<manager>
<name>cxli</name>
<sex>male</sex>
<age>23</age>
<salery>10k</salery>
<priority>hight</priority>
</manager>
<manager>
<name>randy</name>
<sex>male</sex>
<age>23</age>
<salery>10k</salery>
<priority>middle</priority>
</manager>
</managers>
<employees>
<employee>
<name>fengzi</name>
<sex>male</sex>
<age>23</age>
<salery>6k</salery>
</employee>
<employee>
<name>saoxing</name>
<sex>male</sex>
<age>23</age>
<salery>6k</salery>
</employee>
</employees>
</stuffs>
从跟上面的XML,我们可以看出,存储的信息stuff信息,具体分为manager和employee。因此,我们可以讲XML中存储的信息抽象为JAVA类,即Manager类和Employee类;他们都实现了接口Stuff。代码如下:
Stuff接:
package com.xml.domain;
/**
*
* @author 风子
* 员工接口
*
*/
public interface Stuff {}
Manager类:
package com.xml.domain;
public class Manager implements Stuff{
private String name;
private String sex;
private int age;
private String salery;
private String priority;
public Manager(String name, String sex, int age, String salery,String priority) {
this.name = name;
this.sex = sex;
this.age = age;
this.salery = salery;
this.priority = priority;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSalery() {
return salery;
}
public void setSalery(String salery) {
this.salery = salery;
}
public String getPriority() {
return priority;
}
public void setPriority(String priority) {
this.priority = priority;
}
}
Employee类:
package com.xml.domain;
public class Employee implements Stuff{
private String name;
private String sex;
private int age;
private String salery;
public Employee(String name, String sex, int age, String salery) {
this.name = name;
this.sex = sex;
this.age = age;
this.salery = salery;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSalery() {
return salery;
}
public void setSalery(String salery) {
this.salery = salery;
}
}
二、DOM生成和解析XML文档
标签:
原文地址:http://www.cnblogs.com/FZ1314/p/5874116.html