Subversion Repositories DevTools

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2308 mtayler 1
/*
2
 * Created on 24/03/2005
3
 */
4
package DMS;
5
 
6
import javax.xml.parsers.DocumentBuilderFactory;
7
import javax.xml.parsers.DocumentBuilder;
8
import org.w3c.dom.Document;
9
import org.w3c.dom.Node;
10
import org.w3c.dom.NodeList;
11
import java.util.Vector;
12
import java.io.InputStream;
13
 
14
/**
15
 * @author mtayler
16
 */
17
public class XMLParser {
18
 
19
	public XMLParser() {
20
		super();
21
 
22
 
23
	}
24
 
25
	public static Document getDocument(String fileName){
26
		Document doc = null;
27
		try {	
28
		    DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
29
			InputStream is = DMSUtils.getResourceAsStream(fileName);
30
			try {
31
				doc = docBuilder.parse(is);
32
		    } finally {
33
		      is.close();
34
		    }
35
		} catch (Exception e) { 
36
			e.printStackTrace();			
37
		}	
38
 
39
		return doc;
40
	}
41
 
42
 
43
	public static Node getRootNode(String fileName){
44
		Node root = null;
45
		try {			
46
			Document doc = getDocument(fileName);
47
			root = doc.getDocumentElement();			
48
		} catch (Exception e) { 
49
			e.printStackTrace();	
50
		}	
51
		return root;
52
	}
53
 
54
 
55
	public static Node findNode(Node root, String nodeName) {
56
		if (root!=null) {
57
			NodeList list = root.getChildNodes();
58
 
59
			if (list!=null)
60
			for (int i=0;i<list.getLength();i++) {
61
				Node item = list.item(i);
62
				if (item!=null && item.getNodeName().toUpperCase().equals(nodeName.toUpperCase())) {
63
					return item; 
64
				}
65
 
66
			}				
67
		}
68
		return null; //return null if no node with the given name is found 
69
	}
70
 
71
	public static Vector findNodeList(Node root, String nodeName) {
72
		Vector vector = new Vector();			
73
		if (root!=null) {		
74
			NodeList list = root.getChildNodes();
75
 
76
			if (list!=null)
77
			for (int i=0;i<list.getLength();i++) {
78
				Node item = list.item(i);
79
				if (item!=null && item.getNodeName().toUpperCase().equals(nodeName.toUpperCase())) {
80
					vector.add(item);
81
				}		
82
			}			
83
		}
84
		return vector;		
85
	}
86
 
87
 
88
	public static String findNodeValue(Node root, String nodeName) {
89
		Node retNode = findNode(root, nodeName);
90
		if (retNode!=null) {
91
			Node child = retNode.getFirstChild();
92
			if (child!=null) return child.getNodeValue();
93
		}
94
		return "";  
95
	}
96
 
97
	public static boolean findNodeValueBool(Node root, String nodeName) {
98
		String value = DMSUtils.NVL(findNodeValue(root, nodeName),"false");
99
		return value.toLowerCase().trim().equals("true");
100
	}
101
 
102
}