利用dom4j创建XML和解析XML_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 利用dom4j创建XML和解析XML

利用dom4j创建XML和解析XML

 2011/9/21 8:10:34  jeasonjack  http://jeasonjack.iteye.com  我要评论(0)
  • 摘要:packagecom.ylt.mfz.util;importjava.io.File;importjava.io.FileWriter;importjava.io.IOException;importjava.io.Writer;importjava.util.Iterator;importorg.dom4j.Attribute;importorg.dom4j.Document;importorg.dom4j.DocumentException;importorg.dom4j
  • 标签:创建 利用 创建XML XML 解析
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");
	}
}
发表评论
用户名: 匿名