今天我学习了SAX的一些基础知识.
在网上看到了sun技术社区的一篇文章《使用JAXP处理XML文件》
这篇文章网址是
http://gceclub.sun.com.cn/yuanchuang/week-9/jaxp.html
我觉得他的算法太麻烦,自己想了一个简单点的,例子里的xml文件是向作者借来的,thx.
一、xml文件以及源代码
Test.xml
<?xml version="1.0" ?>
<customers>
<customer>
<id>#001</id>
<name>Micke</name>
<address>Najing</address>
</customer>
<customer>
<id>#002</id>
<name>Car</name>
<address>Suzhou</address>
</customer>
<customer>
<id>#003</id>
<name>Jimmy</name>
<address>ChengDu</address>
</customer>
<customer>
<id>#004</id>
<name>Henry</name>
<address>Xi'an</address>
</customer>
</customers>
Customers.java
/*
* Created on 2005-12-9
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.util.*;
/**
* @author Rey
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Customers {
private ArrayList customers;
public Customers() {
customers = new ArrayList();
}
public void addCustomers(Customer c) {
customers.add(c);
}
public String toString() {
String newline = System.getProperty("line.separator");
StringBuffer buf = new StringBuffer();
for (int i = 0; i < customers.size(); i++) {
buf.append(customers.get(i)).append(newline);
}
return buf.toString();
}
}
class Customer {
private String id;
private String name;
private String address;
Customer(String id, String name, String address) {
this.id = id;
this.name = name;
this.address = address;
}
public String getAddress() {
return address;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public void setAddress(String string) {
address = string;
}
public void setId(String string) {
id = string;
}
public void setName(String string) {
name = string;
}
public String toString(){
return "Customer: ID='" + id + "' Name='" + name +
"' Address='" + address + "'";
}
}
Test.java
import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import java.util.*;
/**
* @author Rey
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Test extends DefaultHandler {
private static ArrayList al;
private StringBuffer textBuffer;
public Test() {
al = new ArrayList();
}
public static void main(String[] argv) {
DefaultHandler handler = new Test();
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(new File("Test.xml"), handler);
} catch (Throwable t) {
t.printStackTrace();
}
((Test)handler).showList();
System.exit(0);
}
public void endElement(String namespaceURI, String sName, // simple name
String qName // qualified name
) throws SAXException {
if (qName.equals("id")||qName.equals("name")||qName.equals("address"))
al.add(echoText());
}
public void characters(char[] buf, int offset, int len)
throws SAXException {
String s = new String(buf, offset, len);
if (textBuffer == null) {
textBuffer = new StringBuffer(s);
} else {
textBuffer.append(s);
}
}
private String echoText(){
String s = "" +textBuffer;
textBuffer = null;
return s.trim();
}
public void showList() {
Customers c = new Customers();
String[] temp = (String[])al.toArray(new String[1]);
for (int i = 0, j = 0; i < al.size()/3; i++) {
c.addCustomers(new Customer(temp[j++], temp[j++], temp[j++]));
}
System.out.println(c);
}
}
二、程序输出:
Customer: ID='#001' Name='Micke' Address='Najing'
Customer: ID='#002' Name='Car' Address='Suzhou'
Customer: ID='#003' Name='Jimmy' Address='ChengDu'
Customer: ID='#004' Name='Henry' Address='Xi'an'
三、笔记
在阅读sun公司的文档时,有几个名词好半天都不知道什么意思,查了半天才知道.
关于XML的名称空间,限定名称、前缀和本地部分
名称空间中的元素和属性的名称中以冒号隔开
rdf:description
xlink:type
xsl:template
冒号前的部分叫前缀(prefix)
冒号后的部分叫本地部分(local part)
包括冒号在内的整个部分叫限定名称(qualified name)qName