dom4J生成xml、解析xml、生成xml文件实例

 

使用 dom4J生成与解析xml,直接上测试代码

 

需要导入dom4j-1.6.1.jar包

 

package test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class CreateXml 
{

	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		createFile();
		
		try 
		{
			parserXmlByHtmlXml("html.xml");
		} 
		catch (DocumentException e) 
		{
			e.printStackTrace();
		}
	}

	/**
	 * 创建一个document
	 * @return
	 */
	public static Document createXml()
	{
		//初始化dom对象
		Document document = DocumentHelper.createDocument();
		
		/**
		 * 生成一个html格式的xml用于新手学习使用~
		 */
		// 生成根节点
		Element dCAuthenticate = document.addElement("html");
		
		// 生成DCAuthenticate节点的子节点result
        Element result = dCAuthenticate.addElement("head");
        Element resultTitleElement = result.addElement("title");
        Element resultLinkElement = result.addElement("link");
        
        resultTitleElement.setText("我的首页");
        resultLinkElement.setText("www.baidu.com/css");
        
        Element bodyElement = dCAuthenticate.addElement("body");
        Element div1Element = bodyElement.addElement("div1");
        div1Element.setText("div1");
        Element div2Element = bodyElement.addElement("div2");
        div2Element.setText("div2");
        Element div3Element = bodyElement.addElement("div3");
        div3Element.setText("div3");
        return document;
	}
	
	
	/**
	 * 将document转换成String保存到文件中输出
	 */
	public static void createFile()
	{
		Document doc = CreateXml.createXml();
		//输出xml文件,后面用户dom4j读取用户信息使用
	    String fileName = "html.xml";
		File file = new File(fileName);
		FileOutputStream fos = null;
		try
		{
		    file.createNewFile();
		    fos = new FileOutputStream(file);
		    System.out.println(doc.asXML());
		    fos.write(doc.asXML().getBytes("UTF-8"));
		}
		catch (FileNotFoundException e)
		{
		    e.printStackTrace();
		    System.out.println("create "+fileName+"  failed!");
		}
		catch (IOException e)
		{
		    e.printStackTrace();
		    System.out.println("create "+fileName+"  failed!");
		}
		finally
		{
		    try
		    {
		        fos.close();
		    }
		    catch (IOException e)
		    {
		    	System.out.println("create "+fileName+"  failed!");
		    }
		}
		System.out.println("create '"+fileName+"'  success");
	}
	
	
	/**
	 * 解析xml格式文件
	 * @throws DocumentException 
	 */
	public static void parserXmlByHtmlXml(String xmlName) throws DocumentException
	{
		/**
		 * 注意:(121-134行)在实际应用到工程时,不能这样(先读取生成的xml,在解析这个xml)
		 * 应该直接从Document对象取值后进行解析,这边只会提供参考方案
		 */
		//获取xml名称
		File inputXml=new File(xmlName);   
		SAXReader saxReader = new SAXReader();   
		Document document = saxReader.read(inputXml);
		Element employees=document.getRootElement();
		for(Iterator i = employees.elementIterator(); i.hasNext();)
		{
			Element employee = (Element) i.next();
//			System.out.println(employee.getName());
			
			if ("head".equals(employee.getName()))
			{
				for(Iterator j = employee.elementIterator(); j.hasNext();)
				{
					Element employee_j = (Element) j.next();
//					System.out.println(employee_j.getName());
					if ("title".equals(employee_j.getName()))
					{
						System.out.println(employee_j.getText());
					}
				}
			}
			else if ("body".equals(employee.getName()))
			{
				for(Iterator j = employee.elementIterator(); j.hasNext();)
				{
					Element employee_j = (Element) j.next();
//					System.out.println(employee_j.getName());
					if ("div1".equals(employee_j.getName()))
					{
						System.out.println(employee_j.getText());
					}
				}
			}
		}
		
	}
	
	
	
}

 

 

 

 

你可能感兴趣的:(dom4j)