import java.io.File;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
/**
* @author liujy
*
*/
public class Test {
/**
* @param args
*/
@SuppressWarnings("unchecked")
public void readURLXML(String url){
Document document;
// =DocumentHelper.createDocument();
SAXReader sax=new SAXReader();
try {
document=sax.read(url);
// document=sax.read(new File(url));
List<Node> list=document.selectNodes("rss/channel/item");
List<Element> listElement=document.selectNodes("rss/channel/item");
for(Node node:list){
System.out.println("====="+node.valueOf("title"));
System.out.println("====="+node.valueOf("link"));
}
//获取属性节点
// for(Element node:listElement){
//
//
// System.out.println("====="+node.element("title").getText());
// System.out.println(node.element("title").attributeValue("name"));
//
//
// }
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
Test test=new Test();
test.readURLXML("http://news.baidu.com/n?cmd=1&class=internews&tn=rss&sub=0");
// test.readURLXML("c:/test.xml");
}
}
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
/**
* @author liujy
*
*/
public class Test {
/**
* @param args
*/
@SuppressWarnings("unchecked")
public void readURLXML(String url){
Document document;
// =DocumentHelper.createDocument();
SAXReader sax=new SAXReader();
try {
document=sax.read(url);
// document=sax.read(new File(url));
List<Node> list=document.selectNodes("rss/channel/item");
List<Element> listElement=document.selectNodes("rss/channel/item");
for(Node node:list){
System.out.println("====="+node.valueOf("title"));
System.out.println("====="+node.valueOf("link"));
}
//获取属性节点
// for(Element node:listElement){
//
//
// System.out.println("====="+node.element("title").getText());
// System.out.println(node.element("title").attributeValue("name"));
//
//
// }
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
Test test=new Test();
test.readURLXML("http://news.baidu.com/n?cmd=1&class=internews&tn=rss&sub=0");
// test.readURLXML("c:/test.xml");
}
}