liuxiaolong
2019-05-09 0d1d88cdb668e75ea8609417ac18ae19947e9525
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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;
    }
 
}