1 | /*********************************************************************
|
---|
2 | Copyright (C) 2004 New Zealand Digital Library, http://www.nzdl.org
|
---|
3 |
|
---|
4 | This program is free software; you can redistribute it and/or modify
|
---|
5 | it under the terms of the GNU General Public License as published by
|
---|
6 | the Free Software Foundation; either version 2 of the License, or
|
---|
7 | (at your option) any later version.
|
---|
8 |
|
---|
9 | This program is distributed in the hope that it will be useful,
|
---|
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
12 | GNU General Public License for more details.
|
---|
13 |
|
---|
14 | You should have received a copy of the GNU General Public License
|
---|
15 | along with this program; if not, write to the Free Software
|
---|
16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
17 | ***********************************************************************/
|
---|
18 |
|
---|
19 | package vishnu.server;
|
---|
20 |
|
---|
21 | import org.w3c.dom.Node;
|
---|
22 | import org.w3c.dom.Element;
|
---|
23 | import org.w3c.dom.NodeList;
|
---|
24 |
|
---|
25 | public class XMLUtil {
|
---|
26 |
|
---|
27 | /** returns the (first) child element with the given name */
|
---|
28 | public static Node getChildByTagName(Node n, String name) {
|
---|
29 |
|
---|
30 | Node child = n.getFirstChild();
|
---|
31 | while (child!=null) {
|
---|
32 | if (child.getNodeName().equals(name)) {
|
---|
33 | return child;
|
---|
34 | }
|
---|
35 | child = child.getNextSibling();
|
---|
36 | }
|
---|
37 | return null; //not found
|
---|
38 | }
|
---|
39 |
|
---|
40 | /** takes a list of elements, and returns an array of strings
|
---|
41 | * of the values of attribute att_name */
|
---|
42 | public static String [] getAttributeValuesFromList(Element list,
|
---|
43 | String att_name) {
|
---|
44 |
|
---|
45 | NodeList children = list.getChildNodes();
|
---|
46 |
|
---|
47 | int num_nodes = children.getLength();
|
---|
48 | String []ids = new String[num_nodes];
|
---|
49 | for (int i=0; i<num_nodes; i++) {
|
---|
50 | Element e = (Element)children.item(i);
|
---|
51 | String id = e.getAttribute(att_name);
|
---|
52 | ids[i] = id;
|
---|
53 | }
|
---|
54 |
|
---|
55 | return ids;
|
---|
56 | }
|
---|
57 |
|
---|
58 | /** extracts the text out of a node */
|
---|
59 | public static String getNodeText(Element param) {
|
---|
60 | param.normalize();
|
---|
61 | Node n = param.getFirstChild();
|
---|
62 | while (n!=null && n.getNodeType() !=Node.TEXT_NODE) {
|
---|
63 | n=n.getNextSibling();
|
---|
64 | }
|
---|
65 | if (n == null) {
|
---|
66 | return "";
|
---|
67 | }
|
---|
68 | return n.getNodeValue();
|
---|
69 | }
|
---|
70 |
|
---|
71 | }
|
---|