通过XMLReader 读取服务器的xml 文件(通过sax2)
2011-12-01 18:52
295 查看
本文通过参考http://www.iteye.com/topic/763895 网站
通过读本地磁盘取得xml 可以参考 http://www.iteye.com/topic/763895网站 InputStream input = new FileInputStream(new File("D:\\test\\test.xml"));
有三类:TestRssTesteSax2.java| SaxRssTestService.java|RssItemDTO.java
第一类:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
public class TestRssTesteSax2 {
public TestRssTesteSax2() {
// TODO Auto-generated constructor stub
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String Url = "http://blog.hiredmyway.com/feed";// Url 就是读这个网站的信息
SaxRssTestService sax = new SaxRssTestService();
try {
URL u = new URL(Url);
URLConnection UC = u.openConnection();
/*
* If we don't set the user-agent property sites like Google won't
* let you access their feeds.
*/
UC.setRequestProperty("User-agent", "www.hiredmyway.com");
InputStreamReader r = new InputStreamReader(UC.getInputStream());
List<RssItemDTO> blogs = sax.getBlogs(r);
for(RssItemDTO dto : blogs){
System.out.println(dto.toString());
System.out.println("-------------------------------");
}
System.out.println("count="+blogs.size());
} catch (Exception e) {
}
}
}
-----------------------------------------------------------------
第二类
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
public class SaxRssTestService extends DefaultHandler{
private List<RssItemDTO> blogs = null;
private RssItemDTO blog = null;
private String preTag = null;//作用是记录解析时的上一个节点名称
public List<RssItemDTO> getBlogs(InputStreamReader xmlStream) throws Exception{
SAXParserFactory factory = SAXParserFactory.newInstance();
// SAXParser parser = factory.newSAXParser();
SaxRssTestService handler = new SaxRssTestService();
XMLReader xr = XMLReaderFactory.createXMLReader();
xr.setContentHandler(handler);
xr.setErrorHandler(handler);
// xr.parse(xmlStream, handler);
xr.parse(new InputSource(xmlStream));
return handler.getBlogs();
}
public List<RssItemDTO> getBlogs() {
return blogs;
}
public void setBlogs(List<RssItemDTO> blogs) {
this.blogs = blogs;
}
@Override
public void startDocument() throws SAXException {
blogs = new ArrayList<RssItemDTO>();
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if("item".equals(qName)){
blog = new RssItemDTO();
// blog.setId(Integer.valueOf(attributes.getValue(0)));
}
preTag = qName;//将正在解析的节点名称赋给preTag
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if("item".equals(qName)){
blogs.add(blog);
blog = null;
}
preTag = null;
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (blog !=null) {
if(preTag!=null){
String content = "";
for (int i = start; i < start + length; i++) {
content += ch[i];
}
if("title".equals(preTag)){
blog.setTitle(content);
}else if("pubDate".equals(preTag)){
blog.setPubDate(content);
}else if ("link".equals(preTag)) {
//description
blog.setURL(content);
} else if ("description".equals(preTag)){
if (content !=null && !content.equals("\n")) {
blog.setDescription(content);
}
} else if ("content:encoded".equals(preTag)) {
if (content !=null && !content.equals("\n")) {
blog.setImageUrl(content);
}
}
}
}
}
}
====================================
第三个类:
package com.hiredmyway.index.sax2test;
import org.apache.commons.lang.StringUtils;
import com.hiredmyway.util.string.ShortTextSubString;
import com.hiredmyway.util.string.TitleSubString;
public class RssItemDTO {
private String Title = "";
private String URL = "";
private String description="";
private String pubDate="";
private String imageUrl = "";
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDescription() {
if(StringUtils.isNotBlank(description)){
description = new ShortTextSubString().trimSubString(description);
}
return description;
}
public void setDescription(String description) {
this.description = description;
}
public void setTitle(String Title) {
if(StringUtils.isNotBlank(this.Title)){
this.Title = new TitleSubString().trimSubString(this.Title);
}
this.Title = Title;
}
public void setURL(String URL) {
this.URL = URL;
}
public String toString() {
return this.Title+":"+this.pubDate+":"+this.URL+":"+this.description+":"+this.getImageUrl();
}
public String getTitle() {
return Title;
}
public String getURL() {
return URL;
}
public String getPubDate() {
return pubDate;
}
public void setPubDate(String pubDate) {
this.pubDate = pubDate;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
}
通过读本地磁盘取得xml 可以参考 http://www.iteye.com/topic/763895网站 InputStream input = new FileInputStream(new File("D:\\test\\test.xml"));
有三类:TestRssTesteSax2.java| SaxRssTestService.java|RssItemDTO.java
第一类:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
public class TestRssTesteSax2 {
public TestRssTesteSax2() {
// TODO Auto-generated constructor stub
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String Url = "http://blog.hiredmyway.com/feed";// Url 就是读这个网站的信息
SaxRssTestService sax = new SaxRssTestService();
try {
URL u = new URL(Url);
URLConnection UC = u.openConnection();
/*
* If we don't set the user-agent property sites like Google won't
* let you access their feeds.
*/
UC.setRequestProperty("User-agent", "www.hiredmyway.com");
InputStreamReader r = new InputStreamReader(UC.getInputStream());
List<RssItemDTO> blogs = sax.getBlogs(r);
for(RssItemDTO dto : blogs){
System.out.println(dto.toString());
System.out.println("-------------------------------");
}
System.out.println("count="+blogs.size());
} catch (Exception e) {
}
}
}
-----------------------------------------------------------------
第二类
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
public class SaxRssTestService extends DefaultHandler{
private List<RssItemDTO> blogs = null;
private RssItemDTO blog = null;
private String preTag = null;//作用是记录解析时的上一个节点名称
public List<RssItemDTO> getBlogs(InputStreamReader xmlStream) throws Exception{
SAXParserFactory factory = SAXParserFactory.newInstance();
// SAXParser parser = factory.newSAXParser();
SaxRssTestService handler = new SaxRssTestService();
XMLReader xr = XMLReaderFactory.createXMLReader();
xr.setContentHandler(handler);
xr.setErrorHandler(handler);
// xr.parse(xmlStream, handler);
xr.parse(new InputSource(xmlStream));
return handler.getBlogs();
}
public List<RssItemDTO> getBlogs() {
return blogs;
}
public void setBlogs(List<RssItemDTO> blogs) {
this.blogs = blogs;
}
@Override
public void startDocument() throws SAXException {
blogs = new ArrayList<RssItemDTO>();
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if("item".equals(qName)){
blog = new RssItemDTO();
// blog.setId(Integer.valueOf(attributes.getValue(0)));
}
preTag = qName;//将正在解析的节点名称赋给preTag
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if("item".equals(qName)){
blogs.add(blog);
blog = null;
}
preTag = null;
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (blog !=null) {
if(preTag!=null){
String content = "";
for (int i = start; i < start + length; i++) {
content += ch[i];
}
if("title".equals(preTag)){
blog.setTitle(content);
}else if("pubDate".equals(preTag)){
blog.setPubDate(content);
}else if ("link".equals(preTag)) {
//description
blog.setURL(content);
} else if ("description".equals(preTag)){
if (content !=null && !content.equals("\n")) {
blog.setDescription(content);
}
} else if ("content:encoded".equals(preTag)) {
if (content !=null && !content.equals("\n")) {
blog.setImageUrl(content);
}
}
}
}
}
}
====================================
第三个类:
package com.hiredmyway.index.sax2test;
import org.apache.commons.lang.StringUtils;
import com.hiredmyway.util.string.ShortTextSubString;
import com.hiredmyway.util.string.TitleSubString;
public class RssItemDTO {
private String Title = "";
private String URL = "";
private String description="";
private String pubDate="";
private String imageUrl = "";
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDescription() {
if(StringUtils.isNotBlank(description)){
description = new ShortTextSubString().trimSubString(description);
}
return description;
}
public void setDescription(String description) {
this.description = description;
}
public void setTitle(String Title) {
if(StringUtils.isNotBlank(this.Title)){
this.Title = new TitleSubString().trimSubString(this.Title);
}
this.Title = Title;
}
public void setURL(String URL) {
this.URL = URL;
}
public String toString() {
return this.Title+":"+this.pubDate+":"+this.URL+":"+this.description+":"+this.getImageUrl();
}
public String getTitle() {
return Title;
}
public String getURL() {
return URL;
}
public String getPubDate() {
return pubDate;
}
public void setPubDate(String pubDate) {
this.pubDate = pubDate;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
}
相关文章推荐
- JS通过ajax动态读取xml文件内容的方法
- 通过java读取远程nfs服务器文件
- android应用面试宝典(下);用自定义组件控制循环组件使用bug还添加标志显示答案标志位;通过包名读取配置文件AndroidManifest.xml
- 通过java读取远程nfs服务器文件
- 服务器使用Tomcat配置server.xml文件通过域名直接跳转到项目
- 通过java读取远程nfs服务器文件
- 服务器使用Tomcat配置server.xml文件通过域名直接跳转到项目
- Matlab读取xml文件——OpenCV通过FileStorage存储的矩阵Mat
- Android中通过代码读取xml文件中的内容
- java开发:读取ftp服务器文件通过浏览器下载
- 使用XmlReader读取xml文件之二
- 使用XmlReader读取xml文件
- 通过java读取远程nfs服务器文件
- Android-通过网络获取xml文件使用pull解析得到服务器中的信息(新闻客户端)
- 通过读取web.xml文件中的配置参数连接数据库(原)
- 使用XmlReader读取xml文件
- tomcat服务器通过web.xml映射发布文件
- JS通过ajax动态读取xml文件内容
- 使用XmlReader读取xml文件
- 通过需要授权访问的http读取xml文件