SAX解析xml学习之爬网工具

前端之家收集整理的这篇文章主要介绍了SAX解析xml学习之爬网工具前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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);
					}
				}
			}
		}
	}
}

猜你在找的XML相关文章