以前写的一个API,你参考参考吧 /**
* Transform a java.util.Hashtable object to a org.xml.dom.Document object.
* NOTE: The specified Hashtable's keys must be String type and values's
* fact type must be String type.
* @param dataSet
* @param rootName
* @return Document
*/
public static synchronized Document getDocument(java.util.Hashtable dataSet, String rootName)
{
Document dataDoc = null;
try
{
//Create the document builder
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbFactory.newDocumentBuilder();
//Create the new document(s)
dataDoc = docBuilder.newDocument();
Element dataRoot = dataDoc.createElement(rootName);
dataDoc.appendChild(dataRoot);
Enumeration keyEmr = dataSet.keys();
Enumeration valueEmr = dataSet.elements();
while (keyEmr.hasMoreElements())
{
String sKey = (String)keyEmr.nextElement();
Recorder.myLog.debug("sKey = "+sKey);
Element prop = dataDoc.createElement(sKey);
dataRoot.appendChild(prop);
Object oValue = valueEmr.nextElement();
Recorder.myLog.debug("oValue's true type = "+oValue.getClass().getName());
if (oValue.getClass().getName().equals(Hashtable.class.getName()))
{
Hashtable hValue = (Hashtable)oValue;
Document tempDoc = getDocument(hValue, rootName);
NodeList tempNl = tempDoc.getDocumentElement().getChildNodes();
int nlLen = tempNl.getLength();
Recorder.myLog.debug("nlLen = "+nlLen);
for (int i=0; i<nlLen; i++)
{
Node tempN = tempNl.item(i);
Recorder.myLog.debug("Node = "+tempN);
prop.appendChild(dataDoc.importNode(tempN, true));
}
}
else
{
String sValue = (String)oValue;
Recorder.myLog.debug("sValue = "+sValue);
Node tValue = dataDoc.createTextNode(sValue);
prop.appendChild(tValue);
}
}
}
catch (Exception e)
{
Recorder.myLog.debug("Caught an exception : ", e);
}
return dataDoc;
}
* Transform a java.util.Hashtable object to a org.xml.dom.Document object.
* NOTE: The specified Hashtable's keys must be String type and values's
* fact type must be String type.
* @param dataSet
* @param rootName
* @return Document
*/
public static synchronized Document getDocument(java.util.Hashtable dataSet, String rootName)
{
Document dataDoc = null;
try
{
//Create the document builder
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbFactory.newDocumentBuilder();
//Create the new document(s)
dataDoc = docBuilder.newDocument();
Element dataRoot = dataDoc.createElement(rootName);
dataDoc.appendChild(dataRoot);
Enumeration keyEmr = dataSet.keys();
Enumeration valueEmr = dataSet.elements();
while (keyEmr.hasMoreElements())
{
String sKey = (String)keyEmr.nextElement();
Recorder.myLog.debug("sKey = "+sKey);
Element prop = dataDoc.createElement(sKey);
dataRoot.appendChild(prop);
Object oValue = valueEmr.nextElement();
Recorder.myLog.debug("oValue's true type = "+oValue.getClass().getName());
if (oValue.getClass().getName().equals(Hashtable.class.getName()))
{
Hashtable hValue = (Hashtable)oValue;
Document tempDoc = getDocument(hValue, rootName);
NodeList tempNl = tempDoc.getDocumentElement().getChildNodes();
int nlLen = tempNl.getLength();
Recorder.myLog.debug("nlLen = "+nlLen);
for (int i=0; i<nlLen; i++)
{
Node tempN = tempNl.item(i);
Recorder.myLog.debug("Node = "+tempN);
prop.appendChild(dataDoc.importNode(tempN, true));
}
}
else
{
String sValue = (String)oValue;
Recorder.myLog.debug("sValue = "+sValue);
Node tValue = dataDoc.createTextNode(sValue);
prop.appendChild(tValue);
}
}
}
catch (Exception e)
{
Recorder.myLog.debug("Caught an exception : ", e);
}
return dataDoc;
}
解决方案 »
- 求编译一个javaee的项目
- 一个整型数组,所有的零元素放到最后输出,非零元素顺序不变,怎么做?主要考虑效率
- 使用人人网API的问题
- Struts2和spring,Hibernate集成的问题
- jsp 中 , 在文本框中输入中文,传值变成乱玛 ,不能SQL查询 怎么办
- 为什么JSTL里的<c:import>标签引入同服务器下不同站台下的页面时总是会有这样的错误?
- 200分求一javaMail例子
- JBoss Client 老是出错!!
- 闭关学习j2ee散分
- 请帮忙
- 怎样在数据表中得到Insert后的自动增量字段的值?
- java如何读http://expert.csdn.net/Expert/PostNew.asp?room=5404的网页源码?
公司不会给这个时间的,呵呵
换个做法,一样可以实现!
谢谢楼上的了
放分