<转载于http://zfive.blogbus.com/logs/20959998.html>
package com.yourcompany.struts.user;
import java.io.IOException;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import java.net.URL;
public class Test {
public static void main(String[] args) {
try{
List result = new ArrayList(); //声明一个结果集
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); //解析器工厂类
DocumentBuilder db;
Document doc;
//此DocumentBuilder对象的作用是根据xml文件的url地址生成document对象
db =dbf.newDocumentBuilder(); //得到一个DOM解析器对象;
//File file = new File("students.xml"); //当然,我们也可以读出本地计算机中的xml文档
//doc对象包含需要解析的feed的xml文件
URL file = new URL("http://localhost:8080/rSS/pfocus.xml");
doc = db.parse(file.openStream()); //把解析后的XML,赋给文档对象;
//feed的xml文件的组成单位是item,从doc中取出所有的item
NodeList nl = doc.getElementsByTagName("item"); //多个<item>标签组成一个链表;
for(int i=0;i<nl.getLength();i++)
{
Element eltStu = (Element)nl.item(i);
//此处得到元素中的<title></title>标签实体;
Node titlenode = eltStu.getElementsByTagName("title").item(0);
//返回第一个节点的值;
String title = titlenode.getFirstChild().getNodeValue();
Node linknode = eltStu.getElementsByTagName("link").item(0);
String link = linknode.getFirstChild().getNodeValue();
Node descriptionnode = eltStu.getElementsByTagName("description").item(0);
String description = descriptionnode.getFirstChild().getNodeValue();
Node authornode = eltStu.getElementsByTagName("author").item(0);
String author = authornode.getFirstChild().getNodeValue();
System.out.println(title);
System.out.println(author);
System.out.println(link);
System.out.println(description);
System.out.println("---------");
}
}
catch(ParserConfigurationException e)
{
e.printStackTrace();
}catch(SAXException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
}
}
}
1007

被折叠的 条评论
为什么被折叠?



