public class ReadXML {
public static void getRoot(String fileName){
//使用JDOM首先要指定使用什么解析器
SAXBuilder builder = new SAXBuilder();
//得到Document,我们以后要进行的所有操作都是对这个Document操作的
Document read_doc = builder.build(fileName);
//得到根元素
Element document = read_doc.getRootElement();
System.out.println();
System.out.println("---------NEWSXML--------------");
//读取根目录底下的一层
String webSiteList = document.getChildText("webSite");
String webMaster = document.getChildText("webMaster");
String updatePeri = document.getChildText("updatePeri");
System.out.println("webSite:"+webSiteList);
System.out.println("webMaster:"+webMaster);
System.out.println("updatePeri:"+updatePeri);
System.out.println("----------ITEM START-------------");
//得到元素(节点)的集合:
List itemlist = document.getChildren("item");
//轮循List集合
for(int i = 0;i < itemlist.size();i++) {
Element e = (Element)itemlist.get(i);
//取得元素的值
String title = e.getChildText("title");
String link = e.getChildText("link");
String text = e.getChildText("text");
String image = e.getChildText("image");
String headlineImg = e.getChildText("headlineImg");
String keywords = e.getChildText("keywords");
String category = e.getChildText("category");
String author = e.getChildText("author");
String source = e.getChildText("source");
String pubDate = e.getChildText("pubDate");
System.out.println("title:" + title);
System.out.println("link:" + link);
System.out.println("text:" + text);
System.out.println("image:"+image);
System.out.println("headlineImg:"+headlineImg);
System.out.println("keywords:"+keywords);
System.out.println("category:"+category);
System.out.println("author:"+author);
System.out.println("source:"+source);
System.out.println("pubDate:"+pubDate);
System.out.println("---------------ITEM END---------------");
}
System.out.println();
}
public static void main(String[] args) {
ReadXML.getRoot("d://newsXml.xml");
}
}
public static void getRoot(String fileName){
//使用JDOM首先要指定使用什么解析器
SAXBuilder builder = new SAXBuilder();
//得到Document,我们以后要进行的所有操作都是对这个Document操作的
Document read_doc = builder.build(fileName);
//得到根元素
Element document = read_doc.getRootElement();
System.out.println();
System.out.println("---------NEWSXML--------------");
//读取根目录底下的一层
String webSiteList = document.getChildText("webSite");
String webMaster = document.getChildText("webMaster");
String updatePeri = document.getChildText("updatePeri");
System.out.println("webSite:"+webSiteList);
System.out.println("webMaster:"+webMaster);
System.out.println("updatePeri:"+updatePeri);
System.out.println("----------ITEM START-------------");
//得到元素(节点)的集合:
List itemlist = document.getChildren("item");
//轮循List集合
for(int i = 0;i < itemlist.size();i++) {
Element e = (Element)itemlist.get(i);
//取得元素的值
String title = e.getChildText("title");
String link = e.getChildText("link");
String text = e.getChildText("text");
String image = e.getChildText("image");
String headlineImg = e.getChildText("headlineImg");
String keywords = e.getChildText("keywords");
String category = e.getChildText("category");
String author = e.getChildText("author");
String source = e.getChildText("source");
String pubDate = e.getChildText("pubDate");
System.out.println("title:" + title);
System.out.println("link:" + link);
System.out.println("text:" + text);
System.out.println("image:"+image);
System.out.println("headlineImg:"+headlineImg);
System.out.println("keywords:"+keywords);
System.out.println("category:"+category);
System.out.println("author:"+author);
System.out.println("source:"+source);
System.out.println("pubDate:"+pubDate);
System.out.println("---------------ITEM END---------------");
}
System.out.println();
}
public static void main(String[] args) {
ReadXML.getRoot("d://newsXml.xml");
}
}