package com.cirle.scientific.utils;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class XmlUtil {

	public static String readXMLStr(String xmlString,String prefix) {
		
		String rs="";
		if(xmlString == null || "".equals(xmlString)){
			return rs;
		}
		try {
			//将请求到的数据包装成document
			Document document = DocumentHelper.parseText(xmlString);
			//获取根节点
			Element root = document.getRootElement();
			if (root == null)
				return rs;
			// 获取属性
			List<Element> attrs = root.elements();
			if (attrs != null && attrs.size() > 0) {
				for (Element attr : attrs) {
					if(prefix!=null&&!"".equals(prefix)){
						if(attr.getName().equals(prefix))
							rs=attr.getText();
					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rs;
	}
	
	public static String map2Xmlstring(Map<String,Object> map){
		StringBuffer sb = new StringBuffer("");
		sb.append("<xml>");
		
		Set<String> set = map.keySet();
        for(Iterator<String> it=set.iterator(); it.hasNext();){
        	String key = it.next();
        	Object value = map.get(key);
        	sb.append("<").append(key).append(">");
        	sb.append(value);
        	sb.append("</").append(key).append(">");
        }
		sb.append("</xml>");
		return sb.toString();
	}
}
