注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

2011终点

2011终点

 
 
 

日志

 
 

利用doc4j读取XML  

2010-10-25 21:46:41|  分类: xml |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

1.<?xml version="1.0" encoding="UTF-8"?>   
<ROWDATA>   
    <ROW>   
        <EMPNO>7499</EMPNO>   
        <ENAME firstname="zhang" lastname="shanfeng">ALLEN</ENAME>   
        <JOB type="basic">SALESMAN</JOB>   
        <MGR>7698</MGR>   
        <HIREDATE>1981-2-20</HIREDATE>   
        <SAL>3200.0</SAL>   
        <COMM>300.00</COMM>   
        <DEPTNO>30</DEPTNO>   
    </ROW>   
  
    <ROW>   
  
        <EMPNO>7566</EMPNO>   
        <ENAME>JONES</ENAME>   
        <JOB>MANAGER</JOB>   
        <MGR>7839</MGR>   
        <HIREDATE>1981-4-2</HIREDATE>   
        <SAL>5950.0</SAL>   
        <COMM />   
        <DEPTNO>20</DEPTNO>   
    </ROW>   
       
</ROWDATA>  

 

2.

package cn.wzh.drp.util.other;

import java.io.FileWriter;
import java.io.Writer;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

public class XmlCreator {
 public void createXml() throws Exception {
  String path = "src/cn/wzh/drp/util/other/lvjian.xml";
        // 创建一个空的文档对象  
        Document doc = DocumentHelper.createDocument();  
 
        Element el_rowdata = doc.addElement("ROWDATA");  
        Element el_row = el_rowdata.addElement("ROW");  
        Element el_empno = el_row.addElement("EMPNO");  
        Element el_ename = el_row.addElement("ENAME");
      
 
        el_empno.setText("7521");  
        el_ename.setText("史密斯");
     
 
        Writer w = new FileWriter(path);  
        OutputFormat opf = OutputFormat.createPrettyPrint();  
        opf.setEncoding("GBK");  
 
        XMLWriter xw = new XMLWriter(w, opf);  
        xw.write(doc);  
 
        xw.close();  
    }  
 
    public static void main(String[] args) throws Exception {  
        XmlCreator xc = new XmlCreator();  
        xc.createXml();  
    }  
    /**
    <?xml version="1.0" encoding="GBK" ?>
    - <ROWDATA>
    - <ROW>
      <EMPNO>7521</EMPNO>
      <ENAME>史密斯</ENAME>
      </ROW>
      </ROWDATA>
      */


}

 

3.

package cn.wzh.drp.util.other;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class UpdateXml {
    public void updateXml() throws IOException, DocumentException {  
     String path = "src/cn/wzh/drp/util/other/EMP.xml";
         SAXReader sr = new SAXReader();  
          
         Document doc = sr.read(path);  
  
         List list = doc.selectNodes("ROWDATA/ROW/SAL");  
         for (Object obj : list) {  
             Element ele = (Element) obj;  
             String sal = ele.getText();  
             double n_sal = 0;  
             if (sal != null && !sal.equals("")) {  
                 n_sal = Double.parseDouble(sal);  
             }  
             ele.setText("" + n_sal * 2);  
         }  
  
         OutputFormat opf = OutputFormat.createPrettyPrint();  
         opf.setEncoding("GBK");  
  
         Writer w = new FileWriter(path);  
         XMLWriter xw = new XMLWriter(w, opf);  
         xw.write(doc);  
         xw.close();  
     }  
  
     public static void main(String[] args) throws DocumentException,  
             IOException {  
  
         UpdateXml txr = new UpdateXml();  
         txr.updateXml();  
  
     }

}

4.

实例

package cn.wzh.drp.util.other;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class ReadXml {
  // 分别输出李华和马跃所有书籍的名称和售价  
    public static void test1() {  
        try {  
            SAXReader reader = new SAXReader();  
            Document doc = reader.read("src/system.xml");  
            List list = doc.selectNodes("system/user/books/btype/book");  
            for (Object obj : list) {  
                Element ele = (Element) obj;  
                // 得到书籍的名称  
                Element bookName = ele.element("name");  
                // 得到书籍的售价  
                Element price = ele.element("price");  
                System.out.println(bookName.getText() + ":" + price.getText());  
            }  
        } catch (DocumentException e) {  
            e.printStackTrace();  
        }  
    }  
 
    // 将所有《C++程序设计教程》一书的价格提高50%  
    public static void test2() {  
        try {  
            SAXReader reader = new SAXReader();  
            String file = "src/system.xml";  
            Document doc = reader.read(file);  
            List list = doc.selectNodes("system/user/books/btype/book");  
            for (Object obj : list) {  
                Element ele = (Element) obj;  
                // 得到书籍的名称  
                Element bookName = ele.element("name");  
                if ("C++程序设计教程".equals(bookName.getText())) {  
                    Element price = ele.element("price");  
                    String tmp = price.getText();  
                    double n_price = 0;  
                    if (tmp != null && !tmp.equals("")) {  
                        n_price = Double.parseDouble(tmp)*1.5;  
                    }  
                    price.setText(n_price + "");  
                }  
            }  
            OutputFormat format = OutputFormat.createPrettyPrint();  
            format.setEncoding("GB2312");  
              
            Writer w = new FileWriter(file);  
            XMLWriter xw = new XMLWriter(w,format);  
            xw.write(doc);  
            xw.close();  
              
        } catch (DocumentException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
 
    public static void main(String[] args) {  
        test2();  
    }  
 

}

 




 

  评论这张
 
阅读(879)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017