com.centit.support.xml.XmlUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of centit-utils Show documentation
Show all versions of centit-utils Show documentation
java 常用工具类,作为 apache-commons的补充
/*
* @(#) XmlUtils.java
* Created Date: 2011-11-8
*
* Copyright (c) Centit Co., Ltd
*
* This software is the confidential and proprietary information of
* Centit Co., Ltd. ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in accordance
* with the terms of the license agreement you entered into with
* Centit Co., Ltd.
*/
package com.centit.support.xml;
import org.apache.commons.lang3.StringEscapeUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* XML公用操作类
*
* @author ljy
* @version $Rev$
* $Id$
*/
@SuppressWarnings("unused")
public abstract class XmlUtils {
private static Logger logger = LoggerFactory.getLogger(XmlUtils.class);
private XmlUtils() {
throw new IllegalAccessError("Utility class");
}
public static Document string2xml(String xmlStr) {
// SAXReader saxReader = new SAXReader();
Document xmlDoc = null;
try {
// InputStream in = new ByteArrayInputStream(xmlStr.getBytes());
// InputStreamReader isReader = new InputStreamReader(in, "GBK");
// xmlDoc = saxReader.read(isReader);
xmlDoc = DocumentHelper.parseText(xmlStr);
} catch (DocumentException e) {
logger.error(e.getMessage(), e);
}
// } catch (UnsupportedEncodingException e) {
// logger.error(e.getMessage(),e.getCause());
// }
return xmlDoc;
}
/*
* 替换字符串中特殊字符
*/
public static String encodeString(String strData) {
return StringEscapeUtils.escapeXml11(strData);
/* if (strData == null)
{
return "";
}
StringBuilder xmlData = new StringBuilder();
//byte[] bf = strData.getBytes();
//int sl = bf.length;
for(int i=0 ;i':
xmlData.append(">");
break;
case '"':
xmlData.append(""");
break;
case '\'':
xmlData.append("'");
break;
default:
xmlData.append(strData.charAt(i));
break;
}
}
return strData; */
}
/*
* 还原字符串中特殊字符
*/
public static String decodeString(String xmlData) {
return StringEscapeUtils.unescapeXml(xmlData);
/*String strData = xmlData.replaceAll("<", "<");
strData = strData.replaceAll( ">", ">");
strData = strData.replaceAll("'", "'");
strData = strData.replaceAll(""", "\"");
strData = strData.replaceAll( "&", "&");
return strData; */
}
/* public static Document mergeXMLDocumet(final Document xmlDoc1,final Document xmlDoc2){
Document xmlDoc = DocumentHelper.createDocument(xmlDoc1.getRootElement());
Element element= xmlDoc.getRootElement();
Element element2= xmlDoc2.getRootElement();
if(element2!=null){
List attrs = element2.attributes();
if(attrs != null){
element.setAttributes(attrs);
}
List elements = element2.elements();
if(elements != null){
for(Element ele : elements)
element.add(ele);
}
}
return xmlDoc;
}*/
}