---
title: "Dom 解析"
author: "rainyear"
date: "2012-02-17T17:08:13Z"
tags: []
categories: ["非技术"]
slug: "dom"
draft: true
lastmod: "2012-02-17T17:33:20Z"
id: 1409665
url: "https://rainyear.iteye.com/blog/1409665"
---

<setting><same enable='true'><samevalue>66</samevalue></same></setting>可以
从数据读取出来的内容

假如节点是：
<setting><same enable="true">
<samevalue>66</samevalue>
<automatic enable="false">
<timevalue>99</timevalue>
<timetype>3</timetype>
</automatic><dealwith enable="true">
<notdealwith enable="true"/>
<pendingreview enable="false"/>
<notpass enable="false"/>
<revoke enable="false"/>
</dealwith></same>
</setting>
获取节点中的节点
Element automaticElement = sameElement.element("automatic");
Map<String, String> automaticMap = new HashMap<String, String>();
automaticMap.put("automaticenable", automaticElement.attributeValue("enable"));
automaticMap.put("timevalue", automaticElement.elementText("timevalue"));
automaticMap.put("timetype", automaticElement.elementText("timetype"));
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.xiu.order.core.exception.ManagerException;
import com.xiu.order.dal.orders.dataobject.ConfigDO;
import com.xiu.order.message.TypeEnum;

public class Queue<E> {
	public static void main(String args) throws ManagerException {
		Queue Queue=new Queue();
		Element root = Queue.rootElement("<setting><same enable='true'><samevalue>66</samevalue></same></setting>");
		Map<String, Object> sameOrderMap = new HashMap<String, Object>();
		Element sameElement1 = root.element("same");
		sameOrderMap.put("sameenable", sameElement1.attributeValue("enable"));
		sameOrderMap.put("samevalue", sameElement1.elementText("samevalue"));
		System.out.println(sameOrderMap.toString());
		
		
		Element rootElement = Queue.createRootElement();
		// 创建主节点
		Element sameElement = rootElement.addElement("same");
		sameElement.addAttribute("enable", "true");

		Element samevalueElement = sameElement.addElement("samevalue");
		samevalueElement.setText("66");

		Element automaticElement = sameElement.addElement("automatic");
		automaticElement.addAttribute("enable", "true");
		automaticElement.addElement("timevalue").setText("70");
		automaticElement.addElement("timetype").setText("2");

		//Element similarityElement = sameElement.addElement("similarity");
		//similarityElement.addAttribute("enable", similarity);

		Element dealwithElement = sameElement.addElement("dealwith");
		dealwithElement.addAttribute("enable", "true");
		dealwithElement.addElement("notdealwith").addAttribute("enable", "true");
		dealwithElement.addElement("pendingreview").addAttribute("enable", "false");
		dealwithElement.addElement("notpass").addAttribute("enable",  "false");
		dealwithElement.addElement("revoke").addAttribute("enable",  "false");

		System.out.println(rootElement.asXML().toString()); 
	}

	protected Document genaratorDocument(String text) throws ManagerException {
		try {
			return DocumentHelper.parseText(text);
		} catch (DocumentException e) {
			throw new ManagerException(e);
		}
	}

	protected Element rootElement(String text) throws ManagerException {
		Document document = this.genaratorDocument(text);
		return document.getRootElement();
	}
	
	
	protected Element createRootElement(){
		return createDocument().addElement("setting");
	}
	protected Document createDocument(){
		Document document=DocumentHelper.createDocument();
		document.setXMLEncoding("UTF-8");
		return document;
	}
	 
}