/**
* @功能说明:将传入的字符串转换成 dom4j Document
* @param message 需要转化的字符串,不能为空
* @return org.dom4j.Document
*/
public static org.dom4j.Document string2Dom4J(String message)
{
SAXReader reader = new SAXReader();
org.dom4j.Document documentDOM4J = null;
try
{
documentDOM4J = reader.read(new ByteArrayInputStream(message
.getBytes()));
}
catch (Exception e)
{
logger.error("Parse message error:" + message, e);
}
return documentDOM4J;
}
/**
* @功能说明:将传入的DOM 的 Document转换成字符串
* @param Document 不能为空
* @return message
*/
public static String document2String(Document doc)
{
TransformerFactory tff = TransformerFactory.newInstance();
String xmlStr = null;
try
{
Transformer tf = tff.newTransformer();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
tf.transform(new DOMSource(doc), new StreamResult(bos));
xmlStr = bos.toString();
}
catch (Exception e)
{
logger.debug("Fail document2String:", e);
}
return xmlStr;
}
/**
* 将DOM的Node 转换成 String 2012-1-4,Xgw123485
*/
public static String nodeToString(Node node)
{
StringWriter sw = new StringWriter();
try
{
Transformer t = TransformerFactory.newInstance().newTransformer();
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.transform(new DOMSource(node), new StreamResult(sw));
}
catch (Exception e)
{
logger.error("nodeToString Transformer Exception");
}
return sw.toString();
}
/**
*
* @Function: 将esb消息中的数据,根据CLE的表和字段生成表和字段的对象数组
* @param propertiesValue, 配置的field和value esbmTablefield, CLE 所有的表
* @param esbmTablefield CLE的日志表对象
* @date 下午06:56:25
*
*/
public static TableName_Field[] sqList(Map<String, String> propertiesValue,
Map<String, List<EsbTableField>> esbmTablefield)
{
Iterator<Entry<String, List<EsbTableField>>> iterator = esbmTablefield
.entrySet().iterator();
TableName_Field[] temp_tableName_Fields = new TableName_Field[esbmTablefield
.size()];
TableName_Field tableName_Field = null;
Map.Entry<String, List<EsbTableField>> entry = null;
// 实际的对像长度
int index = 0;
while (iterator.hasNext())
{
entry = iterator.next();
// 将表的字段对应的字段值,写入到tableName_Field中
tableName_Field = setFieldValueMapping(entry, propertiesValue);
if (tableName_Field != null)
{
temp_tableName_Fields[index++] = tableName_Field;
}
}
TableName_Field[] tableName_Fields = null;
// temp_tableName_Fields的大小等于初始化,不要重新拷贝
if (index == esbmTablefield.size())
{
tableName_Fields = temp_tableName_Fields;
}
else
{
tableName_Fields = new TableName_Field[index];
System.arraycopy(temp_tableName_Fields, 0, tableName_Fields, 0,
index);
}
return tableName_Fields;
}
* @功能说明:将传入的字符串转换成 dom4j Document
* @param message 需要转化的字符串,不能为空
* @return org.dom4j.Document
*/
public static org.dom4j.Document string2Dom4J(String message)
{
SAXReader reader = new SAXReader();
org.dom4j.Document documentDOM4J = null;
try
{
documentDOM4J = reader.read(new ByteArrayInputStream(message
.getBytes()));
}
catch (Exception e)
{
logger.error("Parse message error:" + message, e);
}
return documentDOM4J;
}
/**
* @功能说明:将传入的DOM 的 Document转换成字符串
* @param Document 不能为空
* @return message
*/
public static String document2String(Document doc)
{
TransformerFactory tff = TransformerFactory.newInstance();
String xmlStr = null;
try
{
Transformer tf = tff.newTransformer();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
tf.transform(new DOMSource(doc), new StreamResult(bos));
xmlStr = bos.toString();
}
catch (Exception e)
{
logger.debug("Fail document2String:", e);
}
return xmlStr;
}
/**
* 将DOM的Node 转换成 String 2012-1-4,Xgw123485
*/
public static String nodeToString(Node node)
{
StringWriter sw = new StringWriter();
try
{
Transformer t = TransformerFactory.newInstance().newTransformer();
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.transform(new DOMSource(node), new StreamResult(sw));
}
catch (Exception e)
{
logger.error("nodeToString Transformer Exception");
}
return sw.toString();
}
/**
*
* @Function: 将esb消息中的数据,根据CLE的表和字段生成表和字段的对象数组
* @param propertiesValue, 配置的field和value esbmTablefield, CLE 所有的表
* @param esbmTablefield CLE的日志表对象
* @date 下午06:56:25
*
*/
public static TableName_Field[] sqList(Map<String, String> propertiesValue,
Map<String, List<EsbTableField>> esbmTablefield)
{
Iterator<Entry<String, List<EsbTableField>>> iterator = esbmTablefield
.entrySet().iterator();
TableName_Field[] temp_tableName_Fields = new TableName_Field[esbmTablefield
.size()];
TableName_Field tableName_Field = null;
Map.Entry<String, List<EsbTableField>> entry = null;
// 实际的对像长度
int index = 0;
while (iterator.hasNext())
{
entry = iterator.next();
// 将表的字段对应的字段值,写入到tableName_Field中
tableName_Field = setFieldValueMapping(entry, propertiesValue);
if (tableName_Field != null)
{
temp_tableName_Fields[index++] = tableName_Field;
}
}
TableName_Field[] tableName_Fields = null;
// temp_tableName_Fields的大小等于初始化,不要重新拷贝
if (index == esbmTablefield.size())
{
tableName_Fields = temp_tableName_Fields;
}
else
{
tableName_Fields = new TableName_Field[index];
System.arraycopy(temp_tableName_Fields, 0, tableName_Fields, 0,
index);
}
return tableName_Fields;
}
本文详细介绍了如何使用Java中的DOM4J库将字符串转换为DOM4J文档,并提供了将DOM4J文档转换回字符串的方法。同时,通过实例展示了将ESB消息中的数据与CLE表进行映射的过程。
1723

被折叠的 条评论
为什么被折叠?



