当前位置:编程学习 > XML/UML >>

利用dom4j创建XML和解析XML

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 易做图=employee.addElement("易做图");    
        易做图.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) 
            { 
              &nbs

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,