1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.shared_orchestra.util.xml;
20
21 import org.w3c.dom.Element;
22 import org.w3c.dom.Node;
23 import org.w3c.dom.NodeList;
24
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28
29
30
31
32
33 public class XmlUtils
34 {
35 private XmlUtils()
36 {
37
38 }
39
40 public static String getElementText(Element elem)
41 {
42 StringBuffer buf = new StringBuffer();
43 NodeList nodeList = elem.getChildNodes();
44 for (int i = 0, len = nodeList.getLength(); i < len; i++)
45 {
46 Node n = nodeList.item(i);
47 if (n.getNodeType() == Node.TEXT_NODE)
48 {
49 buf.append(n.getNodeValue());
50 }
51 else
52 {
53
54
55 }
56 }
57 return buf.toString();
58 }
59
60
61
62
63
64
65
66
67
68
69
70
71 public static String getChildText(Element elem, String childTagName)
72 {
73 NodeList nodeList = elem.getElementsByTagName(childTagName);
74 int len = nodeList.getLength();
75 if (len == 0)
76 {
77 return null;
78 }
79 else
80 {
81 return getElementText((Element)nodeList.item(len - 1));
82 }
83 }
84
85
86
87
88
89
90
91
92 public static List getChildTextList(Element elem, String childTagName)
93 {
94 NodeList nodeList = elem.getElementsByTagName(childTagName);
95 int len = nodeList.getLength();
96 if (len == 0)
97 {
98 return Collections.EMPTY_LIST;
99 }
100 else
101 {
102 List list = new ArrayList(len);
103 for (int i = 0; i < len; i++)
104 {
105 list.add(getElementText((Element)nodeList.item(i)));
106 }
107 return list;
108 }
109 }
110
111 }