利用dom4j創建XML和解析XML – JAVA編程語言程序開發技術文章

Java代碼 
package com.ylt.mfz.util; 
 
import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.Writer; 
import java.util.Iterator; 
 
import org.dom4j.Attribute; 
import org.dom4j.Document; 
import org.dom4j.DocumentException; 
import org.dom4j.DocumentHelper; 
import org.dom4j.Element; 
import org.dom4j.io.SAXReader; 
import org.dom4j.io.XMLWriter; 
 
public class Dom4jDemo { 
 
    /**
     * 創建xml文件
     * @param fileName
     */ 
    public void createXml(String fileName)  
    {    
        Document document = DocumentHelper.createDocument();  //創建文檔   
        Element employees=document.addElement("employees");    
        Element employee=employees.addElement("employee");    
        Element name= employee.addElement("name");   //添加子節點 
        name.setText("ddvip"); //添加Text值;例:<a>abc</a> 
        name.setAttributeValue("sa", "sa"); //添加屬性;例:<a item="item"></a> 
        Element sex=employee.addElement("sex");    
        sex.setText("m");    
        Element age=employee.addElement("age");    
        age.setText("29");    
        try {    
            Writer fileWriter=new FileWriter(fileName);    
            XMLWriter xmlWriter=new XMLWriter(fileWriter);    
            xmlWriter.write(document);   //寫入文件中 
            xmlWriter.close();   
            } catch (IOException e) {    
                System.out.println(e.getMessage());    
            }    
    }    
     
    /**
     * 讀取xml文件
     * @param fileName
     */ 
    public void parserXml(String fileName)  
    {    
        File inputXml=new File(fileName);    
        SAXReader saxReader = new SAXReader();    
        try {    
            Document document = saxReader.read(inputXml);   //把文件讀入到文檔 
            Element employees=document.getRootElement();   //獲取文檔根節點 
            //System.out.println(employees); 
            //for(Iterator i = employees.elementIterator(); i.hasNext();){    
                //Element employee = (Element) i.next();   
//              System.out.println(employee); 
//              for(Iterator j = employee.elementIterator(); j.hasNext();){    
//                  Element node=(Element) j.next();    
//                  System.out.println(node.getName()+":"+node.getText()+":"); 
//                  for(Iterator m = node.attributeIterator();m.hasNext();) 
//                  {                
//                      Attribute item = (Attribute)m.next(); 
//                      System.out.println(item.getName()+","+item.getValue()); 
//                  } 
//              }    
                this.doublexml(employees); 
            //}    
        } catch (DocumentException e) {    
            System.out.println(e.getMessage());    
        }    
            System.out.println("dom4j parserXml");    
    }    
     
     
    private void doublexml(Element ele) 
    { 
        for(Iterator i = ele.elementIterator();i.hasNext();) 
        { 
            Element node = (Element)i.next(); 
            System.out.println("節點名:"+node.getName()); 
            if(node.attributes()!=null && node.attributes().size()>0) 
            { 
                for(Iterator j = node.attributeIterator();j.hasNext();) 
                { 
                    Attribute item = (Attribute)j.next(); 
                    System.out.print("屬性名:"+item.getName()+"\t屬性值:"+item.getValue()+"\n"); 
                } 
            } 
            if(node.getText().length()>0) 
            { 
                System.out.println("節點值:"+node.getText()); 
            } 
            if(node.elementIterator().hasNext()) 
            { 
                this.doublexml(node); 
            } 
        } 
    } 
     
    public static void main(String[] args) { 
        Dom4jDemo demo = new Dom4jDemo(); 
             
        demo.createXml("d:/sa.xml"); 
             
        demo.parserXml("d:/getchannels[1].xml"); 
    } 

<pre name="code" class="java"></pre><pre name="code" class="java"></pre><pre name="code" class="java"></pre> 

作者“jeasonjack”
 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *