加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

SAX解析xml学习之爬网工具

发布时间:2020-12-16 09:36:33 所属栏目:百科 来源:网络整理
导读: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.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读