java sax 教程_SAX解析实例

//以下代码统计url.xml文件中每个标签出现的次数

package com.app;

import java.io.File;

import java.util.Enumeration;

import java.util.Hashtable;

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

public class TagCounter extends DefaultHandler {

// Hashtable用来记录tag出现的次数

private Hashtable tags;

public void startDocument() throws SAXException {

tags = new Hashtable();

}

// 解析完成后的工作

public void endDocument() throws SAXException {

Enumeration e = tags.keys();

while (e.hasMoreElements()) {

String tag = (String) e.nextElement();

int count = ((Integer) tags.get(tag)).intValue();

System.out.println("Tag occurs " + count + " times");

}

}

// 对每一个开始元属进行处理

public void startElement(String namespaceURI, String localName,

String rawName, Attributes atts) throws SAXException {

String key = rawName;

Object value = tags.get(key);

if (value == null) {

// 如果是新标签,把它添加在Hastable中

tags.put(key, new Integer(1));

} else {

// 如果以前碰到过,得到其计数值,并加1

int count = ((Integer) value).intValue();

count++;

tags.put(key, new Integer(count));

}

}

static public void main(String[] args) {

String filename = null;

filename = "url.xml";

SAXParserFactory spf = SAXParserFactory.newInstance();

SAXParser saxParser = null;

try {

// 创建解析器SAXParser对象

saxParser = spf.newSAXParser();

saxParser.parse(new File(filename), new TagCounter());

} catch (Exception ex) {

ex.printStackTrace();

}

}

}

//以下程序解析url.xml

package com.app;

import java.io.File;

import java.io.IOException;

import java.util.Stack;

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

public class SAXXMLReader extends DefaultHandler {

Stack tags = new Stack();

// 用于保存解析出来的信息

String text = null;

String url = null;

String author = null;

String description = null;

String day = null;

String year = null;

String month = null;

public void endDocument() throws SAXException {

System.out.println("------解析结束--------");

}

public void startDocument() throws SAXException {

System.out.println("------解析开始--------");

}

public void startElement(String p0, String p1, String p2, Attributes p3)

throws SAXException {

tags.push(p2);

}

public void endElement(String p0, String p1, String p2) throws SAXException {

tags.pop();

if (p2.equals("link"))

parser();

}

public void characters(char[] p0, int p1, int p2) throws SAXException {

// 察看栈顶元素,根据元素名称给对应的变量赋值

String tag = (String) tags.peek();

if (tag.equals("text"))

text = new String(p0, p1, p2);

else if (tag.equals("url"))

url = new String(p0, p1, p2);

else if (tag.equals("author"))

author = new String(p0, p1, p2);

else if (tag.equals("day"))

day = new String(p0, p1, p2);

else if (tag.equals("month"))

month = new String(p0, p1, p2);

else if (tag.equals("year"))

year = new String(p0, p1, p2);

else if (tag.equals("description"))

description = new String(p0, p1, p2);

}

private void parser() {

System.out.print("Content: ");

System.out.println(text);

System.out.print("URL: ");

System.out.println(url);

System.out.print("Author: ");

System.out.println(author);

System.out.print("Date: ");

System.out.println(day + "-" + month + "-" + year);

System.out.print("Description: ");

System.out.println(description);

System.out.println();

}

static public void main(String[] args) {

String filename = "url.xml";

SAXParserFactory spf = SAXParserFactory.newInstance();

SAXParser saxParser = null;

try {

saxParser = spf.newSAXParser();

saxParser.parse(new File(filename), new SAXXMLReader());

} catch (Exception ex) {

ex.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值