残雪余香

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

JDOM是一种使用 XML 的独特 Java 工具包,用于快速开发 XML 应用程序。它的设计包含 Java 语言的语法乃至语义。

JDOM包含有以下几个组件:

org.jdom包含了所有的xml文档要素的java类
  org.jdom.adapters包含了与dom适配的java类
  org.jdom.filter包含了xml文档的过滤器类
  org.jdom.input包含了读取xml文档的类
  org.jdom.output包含了写入xml文档的类
  org.jdom.transform包含了将jdomxml文档接口转换为其他xml文档接口
  org.jdom.xpath包含了对xml文档xpath操作的类

测试类:JDomParse.java

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
public class JDomParse { 
    /**  * 打印xmlpath的内容  * @param xmlpath  */ 
    public void printXmlContent(String xmlpath){  
        //用JDOM首先要指定使用什么解析器  
        SAXBuilder builder = new SAXBuilder(false);  
        try {   
            //得到Document,我们以后要进行的所有操作都是对这个Document操作的   
            Document doc = builder.build(xmlpath);   
            //得到根元素:   
            Element books = doc.getRootElement();   
            //得到元素(节点)的集合   
            List booklist = books.getChildren("book");   
            //遍历   
            for (Iterator iter = booklist.iterator(); iter.hasNext();) 
            {    
                Element book = (Element) iter.next();    
                String email = book.getAttributeValue("email");    
                System.out.println(email);    
                String name = book.getChildTextTrim("name");    
                System.out.println(name);    
                //book.getChild("name").setText("bbbb");
                //修改name节点的值   
                String price = book.getChildTextTrim("price");    
                System.out.println(price);   
            }   
            //保存Document的修改到XML文件中
            //   XMLOutputter outputter = new XMLOutputter();
            //   outputter.output(doc, new FileOutputStream(xmlpath));  
        } catch (FileNotFoundException e) {   
            e.printStackTrace();  
        } catch (JDOMException e) {   
            e.printStackTrace();  
        } catch (IOException e) {   
            e.printStackTrace();  
        } 
    } 
    
    /**  
     * 打印xmlpath的结构  
     * @param xmlpath  
     * */ 
    public void printArch(String xmlpath){  
        //用JDOM首先要指定使用什么解析器  
        SAXBuilder builder = new SAXBuilder(false);  
        //得到Document,我们以后要进行的所有操作都是对这个Document操作的  
        try{   
            Document doc = builder.build(xmlpath);   
            //得到根元素:   
            Element root = doc.getRootElement();   
            System.out.println("<"+root.getName()+">");   
            //System.out.println(root.getChildren().size());   
            List children = root.getChildren();   
            Iterator it = children.iterator();   
            while(it.hasNext()){    
                Element e = (Element)it.next();    
                System.out.println("\t<"+e.getName()+">");    
                //找e的子节点    
                Iterator it_e = e.getChildren().iterator();    
                while(it_e.hasNext()){     
                    Element e_c = (Element)it_e.next();     
                    System.out.println("\t\t<"+e_c.getName()+">");    
                    System.out.println("\t\t</"+e_c.getName()+">");    
                }        
                System.out.println("\t</"+e.getName()+">");   
            }   
            System.out.println("</"+root.getName()+">");  
        }catch(Exception e){   
            e.printStackTrace();  
        }   
    } 
    //main方法测试 
    public static void main(String[] args) 
    {  
        JDomParse parser = new JDomParse();  
        //测试printXmlContent()方法 
        String xmlpath = "D:/test.xml";  
        parser.printXmlContent(xmlpath);    
        //测试printArch()  
        String xmlpath1 = "D:/test.xml";  
        parser.printArch(xmlpath1); 
        }
    }
}

所用XML文件test.xml

<?xml version="1.0" encoding="UTF-8"?>
<books>  
    <book email="test@163.com">    
        <name>计算机应用</name>    
        <price>60.0</price>  
    </book>  
    <book email="test2@163.com">    
        <name>网络技术</name>    
        <price>30.0</price>  
    </book>  
</books>

运行结果显示:

 

posted on 2012-12-16 15:10  残雪余香  阅读(351)  评论(0编辑  收藏  举报