package framework.util;
|
|
import java.io.File;
|
import java.io.StringReader;
|
import java.util.LinkedList;
|
import java.util.List;
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
import org.w3c.dom.Document;
|
import org.w3c.dom.Element;
|
import org.w3c.dom.Node;
|
import org.w3c.dom.NodeList;
|
import org.xml.sax.InputSource;
|
|
/**
|
* 转换 xml 与字符串
|
*
|
* @author liuyajun
|
* @time 2014-12-27下午12:56:56
|
*
|
*/
|
public class XmlUtil {
|
|
/**
|
* 获取xml文件的根结点
|
* @param absXmlPath
|
* @return
|
* @throws Exception
|
*/
|
public static Element getXmlFileRoot(String absXmlPath) throws Exception{
|
File file = new File(absXmlPath);
|
|
DocumentBuilderFactory factory = DocumentBuilderFactory
|
.newInstance();
|
factory.setValidating(false);
|
Document doc = factory.newDocumentBuilder().parse(file);
|
|
return doc.getDocumentElement();
|
}
|
|
/**
|
* 获取 xml 字符串的根结点
|
* @param xml
|
* @return
|
*/
|
public static Node getXmlRoot(String xml){
|
xml = xml.trim();
|
if(! xml.startsWith("<?xml version=")){
|
xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + xml;
|
}
|
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
factory.setValidating(false);
|
InputSource source = null;
|
StringReader reader = null;
|
|
try {
|
reader = new StringReader(xml);
|
source = new InputSource(reader);
|
|
Document doc = factory.newDocumentBuilder().parse(source);
|
return doc.getDocumentElement();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}finally{
|
if (reader != null) {
|
reader.close();
|
}
|
}
|
|
return null;
|
}
|
|
|
/**
|
* 得到节点属性值
|
*
|
* @param node
|
* @param name
|
* @return
|
*/
|
public static String getAttributeValue(Node node, String name){
|
if(node==null || name==null || name.trim().length()==0){
|
return "";
|
}
|
name = name.trim();
|
Node attr = node.getAttributes().getNamedItem(name);
|
if(attr==null){
|
return "";
|
}
|
return attr.getNodeValue().trim();
|
}
|
/**
|
* 得到相对于 node 的xmlPath节点的第一个节点文本内容
|
* @param node
|
* @param xmlPath
|
* @return
|
*/
|
public static final String getNodeTextContent(Node node, String xmlPath){
|
Node n = getNode(node, xmlPath);
|
if(n!=null){
|
return n.getTextContent().trim();
|
}
|
return "";
|
}
|
/**
|
* 得到相对于 node 的 xmlPath 节点的第一个节点
|
* @param node
|
* @param xmlPath
|
* @return
|
*/
|
public static final Node getNode(Node node, String xmlPath){
|
Node[] nodes = getNodeArray(node, xmlPath);
|
if(nodes!=null && nodes.length>=1){
|
return nodes[0];
|
}
|
return null;
|
}
|
/**
|
* 根据XML结点路径, 返回节点数组
|
* @param node 结点
|
* @param xmlPath 相对于结点的XML路径
|
* @return
|
*/
|
public static final Node[] getNodeArray(Node node, String xmlPath){
|
if(node==null){
|
return null;
|
}
|
if(xmlPath==null){
|
return new Node[]{node};
|
}
|
//规范路径
|
xmlPath = xmlPath.trim();
|
if(xmlPath.startsWith("/")){
|
xmlPath = xmlPath.substring(1);
|
}
|
if(xmlPath.endsWith("/")){
|
xmlPath = xmlPath.substring(0, xmlPath.length()-1);
|
}
|
|
//分解路径
|
String[] paths = xmlPath.split("/");
|
|
//无路径直接返回本身
|
if(paths.length==0){
|
return new Node[]{node};
|
}
|
|
List<Node> list = new LinkedList<Node>();
|
|
//路径长度为1, 输出下级节点
|
if(paths.length==1){
|
NodeList nodes = node.getChildNodes();
|
|
for(int i=0, size=nodes.getLength(); i<size; i++){
|
if(nodes.item(i).getNodeName().equals(paths[0].trim())){
|
list.add(nodes.item(i));
|
}
|
}
|
|
return (Node[])list.toArray(new Node[0]);
|
}
|
|
Node[] tmp = new Node[]{node};
|
list.clear();
|
|
for(String path : paths){
|
for(Node n : tmp){
|
NodeList nodes = n.getChildNodes();
|
for(int i=0, size=nodes.getLength(); i<size; i++){
|
if(nodes.item(i).getNodeName().equals(path.trim())){
|
list.add(nodes.item(i));
|
}
|
}
|
}
|
|
tmp = (Node[])list.toArray(new Node[0]);
|
list.clear();
|
}
|
|
return tmp;
|
}
|
|
}
|