1 22 package org.jboss.util.xml; 23 24 import java.io.Writer ; 25 import java.util.ArrayList ; 26 import java.util.Iterator ; 27 28 import org.w3c.dom.Document ; 29 import org.w3c.dom.Element ; 30 import org.w3c.dom.Node ; 31 import org.w3c.dom.NodeList ; 32 33 39 public class XmlHelper 40 { 41 public static void write(Writer out, Document dom) 42 throws Exception 43 { 44 new DOMWriter(out).setPrettyprint(true).print(dom); 45 } 46 47 55 public static Iterator getChildrenByTagName(Element element, 56 String tagName) 57 { 58 if (element == null) return null; 59 62 NodeList children = element.getChildNodes(); 63 ArrayList goodChildren = new ArrayList (); 64 for (int i=0; i<children.getLength(); i++) { 65 Node currentChild = children.item(i); 66 if (currentChild.getNodeType() == Node.ELEMENT_NODE && 67 ((Element )currentChild).getTagName().equals(tagName)) { 68 goodChildren.add((Element )currentChild); 69 } 70 } 71 return goodChildren.iterator(); 72 } 73 74 85 public static Element getUniqueChild(Element element, String tagName) 86 throws Exception 87 { 88 Iterator goodChildren = getChildrenByTagName(element, tagName); 89 90 if (goodChildren != null && goodChildren.hasNext()) { 91 Element child = (Element )goodChildren.next(); 92 if (goodChildren.hasNext()) { 93 throw new Exception 94 ("expected only one " + tagName + " tag"); 95 } 96 return child; 97 } else { 98 throw new Exception 99 ("expected one " + tagName + " tag"); 100 } 101 } 102 103 112 public static Element getOptionalChild(Element element, String tagName) 113 throws Exception 114 { 115 return getOptionalChild(element, tagName, null); 116 } 117 118 129 public static Element getOptionalChild(Element element, 130 String tagName, 131 Element defaultElement) 132 throws Exception 133 { 134 Iterator goodChildren = getChildrenByTagName(element, tagName); 135 136 if (goodChildren != null && goodChildren.hasNext()) { 137 Element child = (Element )goodChildren.next(); 138 if (goodChildren.hasNext()) { 139 throw new Exception 140 ("expected only one " + tagName + " tag"); 141 } 142 return child; 143 } else { 144 return defaultElement; 145 } 146 } 147 148 154 public static String getElementContent(final Element element) 155 throws Exception 156 { 157 return getElementContent(element, null); 158 } 159 160 167 public static String getElementContent(Element element, String defaultStr) 168 throws Exception 169 { 170 if (element == null) 171 return defaultStr; 172 173 NodeList children = element.getChildNodes(); 174 String result = ""; 175 for (int i = 0; i < children.getLength(); i++) 176 { 177 if (children.item(i).getNodeType() == Node.TEXT_NODE || 178 children.item(i).getNodeType() == Node.CDATA_SECTION_NODE) 179 { 180 result += children.item(i).getNodeValue(); 181 } 182 else if( children.item(i).getNodeType() == Node.COMMENT_NODE ) 183 { 184 } 186 } 187 return result.trim(); 188 } 189 190 197 public static String getUniqueChildContent(Element element, 198 String tagName) 199 throws Exception 200 { 201 return getElementContent(getUniqueChild(element, tagName)); 202 } 203 204 211 public static String getOptionalChildContent(Element element, 212 String tagName) 213 throws Exception 214 { 215 return getElementContent(getOptionalChild(element, tagName)); 216 } 217 218 public static boolean getOptionalChildBooleanContent(Element element, String name) throws Exception 219 { 220 Element child = getOptionalChild(element, name); 221 if(child != null) 222 { 223 String value = getElementContent(child).toLowerCase(); 224 return value.equals("true") || value.equals("yes"); 225 } 226 227 return false; 228 } 229 230 231 } 232 233 234 | Popular Tags |