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);
}
}
}
}
}
}
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);
}
}
}
}
}
}
相关文章推荐
- android XML解析学习——Java Sax
- 用SAX方式解析XML(使用Dom4J工具)--第二天
- android 学习笔记 SAX 解析XML数据
- XML学习总结-DOM和SAX解析-综合案例-(四)
- Android 个人学习笔记之---SAX解析XML文件
- android XML解析学习——android.Sax
- java xml解析 学习笔记(2)——SAX
- SAX解析xml学习笔记[1]
- android学习——使用SAX、DOM 和 PULL 解析xml文件,及使用pull生成xml文件
- XML学习总结-SAX解析-(三)
- XML学习之SAX解析XML文件
- java学习脚印:SAX解析xml时命名空间以及参数问题(敏捷版)
- java语言连接mysql数据库并利用XML解析工具DOM生成XML文件,然后利用DOM SAX对所生成XML文件里的详细信息进行解析
- XML语言学习4--SAX解析
- Android XML解析学习——Sax方式
- 【转】android XML解析学习——Java Sax
- Android学习指南之三十一:Android中使用SAX和pull方式解析XML
- Android XML解析学习——Sax方式(续) .
- JAXP,sax与dom4j解析xml文档及XPath在文档内容定位中的应用(持续更新,初学者的野路子、参考和想当然,不断学习和专业化)
- Java学习笔记--xml构造与解析之Sax的使用