xml工具类
1
package com.util;
import java.io.StringWriter;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
public class XmlUtils {
public static String ObjectToXML(Object object){
Serializer JQserializer = new Persister();
StringWriter sw = new StringWriter();
try {
JQserializer.write(object, sw);
} catch (Exception e) {
e.printStackTrace();
}
return sw.toString();
}
public static Object parseXml(Class clz , String requestXML){
Persister serializer = new Persister();
Object object = null;
try {
object = serializer.read(clz, requestXML);
} catch (Exception e) {
e.printStackTrace();
}
return object;
}
public static Object nullToEmpty(Object object){
if(object == null){
return "";
}else{
return object;
}
}
public static Double getDoubleFormat(String str){
if ((!"".equals(str)) &&(str != null)) {
return Double.valueOf(str);
}else{
return Double.valueOf("0.00");
}
}
public static Long getLongFormat(String str){
if ((!"".equals(str)) &&(str != null)) {
return Long.valueOf(str);
}else{
return Long.valueOf("0");
}
}
public static Integer getIntFormat(String str){
if ((!"".equals(str)) &&(str != null)) {
return Integer.valueOf(str);
}else{
return Integer.valueOf("0");
}
}
public static String getToStringFromDouble(Double d){
String str = "";
if (d != null) {
str = d.toString();
}
return str;
}
}
2
package com.yinhai.common.webservice;
import java.io.ByteArrayOutputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.log4j.Logger;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class Xmlutils {
private Logger logger = Logger.getLogger(super.getClass().getName());
public String xmltostring(Document docReturn) {
Format format = Format.getPrettyFormat();
format.setEncoding("UTF-8");
XMLOutputter outp = new XMLOutputter(format);
return outp.outputString(docReturn);
}
public String xmltostring(Element docReturn) {
Format format = Format.getCompactFormat();
format.setEncoding("UTF-8");
XMLOutputter outp = new XMLOutputter(format);
return outp.outputString(docReturn);
}
public String outXmlandXsl(String inxml, String inxsl) throws Exception {
StringReader sreader = new StringReader(inxml);
StringReader xslsreader = new StringReader(inxsl);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
StreamResult outStreamResult = new StreamResult(bos);
SAXBuilder builder = new SAXBuilder();
Reader rdargxml = new StringReader(inxml);
Document docArgxml = null;
try {
docArgxml = builder.build(rdargxml);
} catch (Exception e) {
System.out.println("传入转换有错的xml:" + inxsl);
throw new Exception("输入的xml的格式有错:" + e.getMessage());
}
Source xmlSource = new StreamSource(sreader);
Source xsltSource = new StreamSource(xslsreader);
TransformerFactory transFact = TransformerFactory.newInstance();
Transformer trans = transFact.newTransformer(xsltSource);
try {
trans.transform(xmlSource, outStreamResult);
} catch (Exception e) {
System.out.println("传入转换有错的xslt:" + inxml);
throw new Exception("输入的xslt的语法有错请检查");
}
return new String(bos.toByteArray(), "UTF-8");
}
public Element getElementByPath(Element el, String sName) throws Exception {
int j;
int pos;
Element epath = null;
int i = 0;
String[] arrName = new String[10];
try {
while ((pos = sName.indexOf(".")) > 0) {
arrName[i] = sName.substring(0, pos);
sName = sName.substring(pos + 1);
++i;
}
arrName[i] = sName;
epath = el.getChild(arrName[0]);
if (epath == null) {
this.logger.error("输入的xml没有" + arrName[0] + "节点!");
throw new Exception("输入的xml没有" + arrName[0] + "节点!");
}
for (j = 1; j <= i; ++j) {
epath = epath.getChild(arrName[j]);
if (epath == null) {
this.logger.error("输入的xml没有" + arrName[j] + "节点!");
throw new Exception("输入的xml没有" + arrName[j] + "节点!");
}
}
} catch (Exception e) {
this.logger.info("取" + sName + "出错!");
throw new Exception("输入的xml的数据格式不正确!");
}
return epath;
}
public String getValueFromXML(String sXML, String sName, int num)
throws Exception {
int pos;
String returnstr;
int i = 0;
String[] arrName = new String[10];
while ((pos = sName.indexOf(".")) > 0) {
arrName[i] = sName.substring(0, pos);
sName = sName.substring(pos + 1);
++i;
}
arrName[i] = sName;
SAXBuilder builder = new SAXBuilder();
Reader rd = new StringReader(sXML);
Document doc = builder.build(rd);
Element root = doc.getRootElement();
if (!(root.getName().equalsIgnoreCase(arrName[0]))) {
this.logger.error("输入的xml没有" + arrName[0] + "节点!");
throw new Exception("输入的xml的数据格式不正确!");
}
label318: for (int j = 1; j <= i; ++j) {
try {
if (j < i) {
root = (Element) root.getChildren(arrName[j]).get(0);
} else {
root = (Element) root.getChildren(arrName[j]).get(num - 1);
}
if (root != null)
break label318;
this.logger.error("输入的xml没有" + arrName[j] + "节点!");
throw new Exception("输入的xml的数据格式不正确!");
} catch (Exception e) {
this.logger.error("输入的xml没有" + arrName[j] + "节点!");
throw new Exception("没有获取到节点:" + arrName[j]);
}
}
List Children = root.getChildren();
if (Children.isEmpty()) {
returnstr = root.getText();
} else {
returnstr = xmltostring(root);
}
return returnstr;
}
public String getValueFromXML(String sXML, String sName) throws Exception {
return getValueFromXML(sXML, sName, 1);
}
public String getAllChildText(Element element) {
List child = element.getChildren();
StringBuffer sb = new StringBuffer("");
for (Iterator localIterator = child.iterator(); localIterator.hasNext();) {
List listAttributes;
int iAttributes;
int j;
Attribute attribute;
Element childEle = (Element) localIterator.next();
String s = "";
if (childEle.getChildren().size() > 0) {
listAttributes = childEle.getAttributes();
iAttributes = listAttributes.size();
for (j = 0; j < iAttributes; ++j) {
attribute = (Attribute) listAttributes.get(j);
s = s + " " + attribute.getName() + "=\""
+ attribute.getValue() + "\"";
}
sb.append("<" + childEle.getName() + s + ">"
+ getAllChildText(childEle) + "</" + childEle.getName()
+ ">");
} else {
listAttributes = childEle.getAttributes();
iAttributes = listAttributes.size();
for (j = 0; j < iAttributes; ++j) {
attribute = (Attribute) listAttributes.get(j);
s = s + " " + attribute.getName() + "=\""
+ attribute.getValue() + "\"";
}
sb.append("<" + childEle.getName() + s + ">"
+ childEle.getText() + "</" + childEle.getName() + ">");
}
}
return sb.toString();
}
public boolean contains(String[] stringArray, String source) {
List tempList = Arrays.asList(stringArray);
return (!(tempList.contains(source)));
}
public String addCDATA(String message) {
if (message.toLowerCase().startsWith("<![cdata[")) {
return message;
}
return "<![CDATA[" + message + "]]>";
}
}