1 /***
2 * XMLHelper.java
3 *
4 * $Author: mballesteros $
5 * $Date: 2003/11/28 19:18:04 $
6 * $Revision: 1.1 $
7 */
8 package net.sf.jec.util;
9
10 import org.w3c.dom.Element;
11 import org.w3c.dom.Node;
12 import org.w3c.dom.NodeList;
13
14 /*** Helper class for commond XML handling tasks
15 *
16 * @author mballesteros
17 */
18 public class XMLHelper {
19
20 /*** Returns the concatenation of every element's child text node
21 */
22 public static String getText(Element el) {
23 StringBuffer sb = new StringBuffer();
24 NodeList nl = el.getChildNodes();
25 int len = nl.getLength();
26 Node child;
27 for (int i = 0; i < len; i++) {
28 child = nl.item(i);
29 if (child.getNodeType() == Node.TEXT_NODE) {
30 sb.append(child.getNodeValue());
31 }
32 }
33 return sb.toString();
34 }
35
36 /*** Sets element's text, erasing previous value
37 */
38 public static void setText(Element el, String value) {
39 NodeList nl = el.getChildNodes();
40 int len = nl.getLength();
41 Node child;
42 for (int i = 0; i < len; i++) {
43 child = nl.item(i);
44 if (child.getNodeType() == Node.TEXT_NODE) el.removeChild(child);
45 }
46 el.insertBefore(el.getOwnerDocument().createTextNode(value), nl.item(0));
47 }
48
49 /*** Returns children of a given Element. If there are several children, a
50 * NodeList is returned. If there is only one child, it is returned. If there
51 * is no child for that name, a new one is created and returned.
52 */
53 public static Object getChild(Element el, String childName) {
54 NodeList nl = el.getElementsByTagName(childName);
55 switch (nl.getLength()) {
56 case 0:
57 Node child = el.getOwnerDocument().createElement(childName);
58 el.appendChild(child);
59 return child;
60 case 1:
61 return nl.item(0);
62 default:
63 return nl;
64 }
65 }
66
67 /*** Creates a new child element with the given name, removing any existing
68 * element for that name. If value is a Node, it will be added as a
69 * child to the new element. If value is a String, a new text node will be
70 * created and added to the new element.
71 */
72 public static void setChild(Element el, String childName, Object value) {
73 NodeList nl = el.getElementsByTagName(childName);
74 if (nl.getLength() > 0) {
75 el = (Element)nl.item(0);
76 } else {
77 Element parent = el;
78 el = parent.getOwnerDocument().createElement(childName);
79 parent.appendChild(el);
80 }
81 if (value instanceof Node) {
82 el.appendChild((Node)value);
83 } else {
84 setText(el, value.toString());
85 }
86 }
87
88 /*** Creates an empty brother (same parent, same tag name)
89 */
90 public static Element createBrother(Element el) {
91 return createBrother(el, null);
92 }
93
94 /*** Creates an empty brother (same parent, same tag name) with a text node
95 */
96 public static Element createBrother(Element el, String text) {
97 Node parent = el.getParentNode();
98 el = el.getOwnerDocument().createElement(el.getTagName());
99 parent.appendChild(el);
100 if (text != null) setText(el, text);
101 return el;
102 }
103 }
This page was automatically generated by Maven