/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package my.v2ch02;
import java.io.InputStream;
import java.net.URL;
import javax.xml.parsers.*;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
*
* @author Admini
*/
public class SAXTest {
public static void main(String[] ages) throws Exception {
String url = "";
if (ages.length == 0) {
url = "http://www.baidu.com";
System.out.println("Using:" + url);
} else {
url = ages[0];
}
//创建一个处理器
DefaultHandler handler = new DefaultHandler() {
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
System.out.println("uri = " + uri);
System.out.println("qName = " + qName);
if (localName.equals("a") && attributes != null) {
for (int i = 0; i < attributes.getLength(); i++) {
String aname = attributes.getLocalName(i);
if (aname.equals("href")) {
System.out.println(attributes.getValue(i));
}
}
}
}
};
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
SAXParser parser = factory.newSAXParser();
InputStream in = new URL(url).openStream();
parser.parse(in, handler);
}
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package my.v2ch02;
import java.io.InputStream;
import java.net.URL;
import javax.xml.parsers.*;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
*
* @author Admini
*/
public class SAXTest {
public static void main(String[] ages) throws Exception {
String url = "";
if (ages.length == 0) {
url = "http://www.baidu.com";
System.out.println("Using:" + url);
} else {
url = ages[0];
}
//创建一个处理器
DefaultHandler handler = new DefaultHandler() {
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
System.out.println("uri = " + uri);
System.out.println("qName = " + qName);
if (localName.equals("a") && attributes != null) {
for (int i = 0; i < attributes.getLength(); i++) {
String aname = attributes.getLocalName(i);
if (aname.equals("href")) {
System.out.println(attributes.getValue(i));
}
}
}
}
};
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
SAXParser parser = factory.newSAXParser();
InputStream in = new URL(url).openStream();
parser.parse(in, handler);
}
}