jdom的使用

 前些日志去面试,让读取一个xml文件,由于紧张没有写出来。回家后一会就搞定了。郁闷啊。这个太简单的一道题目啊。

 

import  java.io.IOException;
import  java.net.MalformedURLException;
import  java.net.URL;
import  java.util.List;

import  org.jdom.Document;
import  org.jdom.Element;
import  org.jdom.JDOMException;
import  org.jdom.input.SAXBuilder;

public   class  testjdom  {

    
public static void main(String[] args) {
        String url 
= "http://temp.csdn.net/Feed.aspx?Column=0d928e6c-1d5f-4d80-a901-fc01ee679f07";
        
try {
            
//读取rss文件
            URL u = new URL(url);
            SAXBuilder saxdom 
= new SAXBuilder();
            Document document 
= saxdom.build(u);
            
//得到xml文件的根目录
            Element rss = document.getRootElement();
            Element channel 
= rss.getChild("channel");
            
//得到xml文件的子节点
            Element title = channel.getChild("title");
            
//得到xml节点内的内容
            System.out.println(title.getText());
            Element first_link 
= channel.getChild("link");
            System.out.println(first_link.getText());
            List items 
= channel.getChildren("item");
            
int size = items.size();
            
for(int i = 0; i < size; i ++){
                Element item 
= (Element)items.get(i);
                Element link 
= item.getChild("link");
                String str_link 
= link.getText();
                System.out.println(str_link);
            }

        }
 catch (MalformedURLException e) {
            e.printStackTrace();
        }
 catch (JDOMException e) {
            e.printStackTrace();
        }
 catch (IOException e) {
            e.printStackTrace();
        }

    }

}


你可能感兴趣的:(xml,String,面试,url,Class,import)