XML工具类
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;


import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;


/**
 * <p>
 * </p>
 * 
 * @author Changsoul.Wu
 * @date 2014-3-29 涓婂崍12:32:47
 */
public class XmlUtils {
private static final Logger Log = Logger.getLogger(XmlUtils.class);


private static DocumentBuilderFactory dbFactory;


public static Document readXml(InputStream in) {


if (dbFactory == null) {
dbFactory = DocumentBuilderFactory.newInstance();
dbFactory.setIgnoringElementContentWhitespace(true);
dbFactory.setCoalescing(true);
dbFactory.setIgnoringComments(true);
}


if (in == null)
return null;


try {
return dbFactory.newDocumentBuilder().parse(in);
} catch (SAXException e) {
} catch (IOException e) {
} catch (ParserConfigurationException e) {
}


return null;
}


public static Document readXml(String xmltext) {


if (dbFactory == null) {
dbFactory = DocumentBuilderFactory.newInstance();
dbFactory.setIgnoringElementContentWhitespace(true);
dbFactory.setCoalescing(true);
dbFactory.setIgnoringComments(true);
}


if (xmltext == null || xmltext.trim().equals(""))
return null;


try {
StringReader sr = new StringReader(xmltext);
InputSource is = new InputSource(sr);
return dbFactory.newDocumentBuilder().parse(is);
} catch (SAXException e) {
} catch (IOException e) {
} catch (ParserConfigurationException e) {
}


return null;
}


public static String getVaue(Document doc, String key) {


if (doc == null) {
Log.error("XML Document is null");
return null;
}


NodeList nList = doc.getElementsByTagName(key);


if (nList.getLength() > 0) {


Node node = nList.item(0).getFirstChild();


if (node != null) {
return node.getNodeValue();
}
}


return null;
}


public static NamedNodeMap getAttributes(Document doc, String key) {


if (doc == null) {
Log.error("XML Document is null");
return null;
}


Node node = getNode(doc, key);


if (node != null) {


return node.getAttributes();
}


return null;
}


public static String getAttribute(NamedNodeMap n, String key) {


if (n == null) {
return null;
}


Node node = n.getNamedItem(key);


if (node != null) {
return node.getNodeValue();
}


return null;
}


public static String getAttribute(Document doc, String key, String key2) {


if (doc == null) {
return null;
}


return getAttribute(getAttributes(doc, key), key2);
}


private static Node getNode(Document doc, String key) {


if (doc == null) {
Log.error("XML Document is null");
return null;
}


NodeList nList = doc.getElementsByTagName(key);


if (nList.getLength() > 0) {


Node node = nList.item(0);
return node;
}


return null;
}


public static String toString(Document doc) {
Transformer transformer;
try {
transformer = TransformerFactory.newInstance().newTransformer();
// transformer.setOutputProperty(OutputKeys.INDENT, "yes");
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
String xmlString = result.getWriter().toString();
return xmlString.toString();
} catch (TransformerConfigurationException e) {
Log.error(e.getMessage(), e);
} catch (TransformerFactoryConfigurationError e) {
Log.error(e.getMessage(), e);
} catch (TransformerException e) {
Log.error(e.getMessage(), e);
}
return null;
}


public static Map<String, Object> convertNodeToMap(Node node) {
NodeList nodeList = node.getChildNodes();


Map<String, Object> map = new HashMap<String, Object>(nodeList.getLength());


for (int i = 0; i < nodeList.getLength(); i++) {
Node nodec = nodeList.item(i);


String key = nodec.getNodeName();


Object value = null;
if (nodec.hasChildNodes()) {


NodeList nodeListc = nodec.getChildNodes();
if (nodeListc.getLength() == 1) {
Node noded = nodeListc.item(0);


short type = noded.getNodeType();


if (type == 3 || type == 4) {
value = noded.getNodeValue();
}


if (noded.getNodeType() == 1) {
value = convertNodeToMap(nodec);
}
} else {
value = convertNodeToMap(nodec);
}
}


map.put(key, value);
}


return map;
}


public static String append(String key, Object value) {
StringBuilder x = new StringBuilder();
x.append("<").append(key).append(">").append(value).append("</").append(key).append(">");
return x.toString();
}


public static String appendCDATA(String key, Object value) {
StringBuilder x = new StringBuilder();
x.append("<").append(key).append("><![CDATA[").append(value).append("]]></").append(key).append(">");
return x.toString();
}


public static String appendStartTag(String key) {
StringBuilder x = new StringBuilder();
x.append("<").append(key).append(">");
return x.toString();
}


public static String appendEndTag(String key) {
StringBuilder x = new StringBuilder();
x.append("</").append(key).append(">");
return x.toString();
}


@SuppressWarnings("unchecked")
public static String buildString(Map<String, Object> mapc) {
StringBuilder xx = new StringBuilder();


for (Map.Entry<String, Object> entry : mapc.entrySet()) {
Object v = entry.getValue();


if (v != null) {
if (v instanceof Map) {


xx.append(append(entry.getKey(), buildString((Map<String, Object>) v)));
} else if (v instanceof List<?>) {


List<?> list = (List<?>) v;


xx.append(appendStartTag(entry.getKey()));


if (list.size() > 0) {
Object type = list.get(0);


if (type instanceof XmlObject) {
List<XmlObject> listXmlObject = (List<XmlObject>) list;
for (XmlObject x : listXmlObject) {
xx.append(buildString(x.buildMap()));
}
} else if (type instanceof Map) {
List<Map<String, Object>> listMapObject = (List<Map<String, Object>>) list;
for (Map<String, Object> m : listMapObject) {
xx.append(buildString(m));
}
}
}


xx.append(appendEndTag(entry.getKey()));
} else if (isNumeric(v.toString())) {


xx.append(append(entry.getKey(), v));
} else {
xx.append(append(entry.getKey(), v));
// xx.append(appendCDATA(entry.getKey(), v));
}
} else {
xx.append(append(entry.getKey(), ""));
}
}
return xx.toString();

}

    public static boolean isNumeric(String str) {
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
}

}

阅读更多
文章标签: xml工具类
个人分类: java开发
想对作者说点什么? 我来说一句

Java解析XML工具类--(java源码)

2010年01月14日 1KB 下载

java通用解析XML工具类

2008年03月24日 2.35MB 下载

没有更多推荐了,返回首页

不良信息举报

XML工具类

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭