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