zhuguifei
2026-03-10 2c1fd10c6fbabb8e9f0e9f07fe66fb36c008e883
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
package com.shlanbao.tzsc.utils.tools;
 
import java.io.File;
import java.util.ArrayList;
import java.util.List;
 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
 
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
 * xml解析
 * @author Leejean
 * @create 2014年12月12日下午4:46:42
 */
public class XmlUtil {
    
    private static Logger log = Logger.getLogger(XmlUtil.class);
    
    public static NodeList getRootNodes(String xmlPath,String root){
        try {
            File f=new File(xmlPath); 
            DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); 
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(f); 
            return doc.getElementsByTagName(root); 
        } catch (Exception e) {
            log.error("解析xml异常", e);
        } 
        return null;
    }
    /**
     * 获得节点值
     * @author Leejean
     * @create 2014年12月11日下午1:46:49
     * @param node
     * @return
     */
    public static String getNodeValue(Node node){
        return node.getTextContent();
    }
    public static List<Node> getSubNodes(Node node){
        NodeList nodeList = node.getChildNodes();
        List<Node> nodes = new ArrayList<Node>();
        for (int i = 0; i < nodeList.getLength(); i++) {
            nodes.add(nodeList.item(i));
        }
        return nodes;
    }
    /**
     * 根据节点名称获得节点值
     * @author Leejean
     * @date 2015年3月2日 下午3:24:04 
     * @param parentNode 父级节点
     * @param nodeName
     * @return
     */
    public static String getValueByNodeName(Node parentNode,String nodeName){
        NodeList nodeList = parentNode.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            if(nodeList.item(i).getNodeName().equalsIgnoreCase(nodeName)){
                return nodeList.item(i).getTextContent();
            }
        }
        return null;
    }
    public static Node getNodeByNodeName(Node parentNode,String nodeName){
        NodeList nodeList = parentNode.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            if(nodeList.item(i).getNodeName().equalsIgnoreCase(nodeName)){
                return nodeList.item(i);
            }
        }
        return null;
    }
}