您的位置:首页 > 其它

SAX解析xml学习之爬网工具

2014-08-16 09:30 239 查看
package af.qian.test;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import org.xml.sax.*;
import org.xml.sax.helpers.*;

import junit.framework.TestCase;

public class XMLTest extends TestCase {
/**
* sax解析xml
* @throws ParserConfigurationException
* @throws SAXException
* @throws MalformedURLException
* @throws IOException
*/
public void test_001() throws ParserConfigurationException, SAXException, MalformedURLException, IOException {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
SAXParser parser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler(){
public void startElement(String namespaceURI,String lname,String qname,Attributes attrs){
if(lname.equals("a")&&attrs!=null){
for(int i=0;i<attrs.getLength();i++){
String aname = attrs.getLocalName(i);
if(aname.equals("href")) System.out.println(attrs.getValue(i));
}
}
}

};
InputStream in = new URL("http://www.w3c.org").openStream();
parser.parse(in, handler);
}
/**
* stax测试xml程序
* @throws IOException
* @throws XMLStreamException
*/
public void test_002() throws IOException, XMLStreamException{
URL url= new URL("http://www.w3c.org");
InputStream in = url.openStream();
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader parser = factory.createXMLStreamReader(in);
while(parser.hasNext()){
int event = parser.next();
if(event == XMLStreamConstants.START_ELEMENT){
if(parser.getLocalName().equals("a")){
String href = parser.getAttributeValue(null, "href");
if(href!=null){
System.out.println(href);
}
}
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: