1 29 30 package nextapp.echo2.app.util; 31 32 import java.util.ArrayList ; 33 import java.util.List ; 34 35 import org.w3c.dom.Element ; 36 import org.w3c.dom.Node ; 37 import org.w3c.dom.NodeList ; 38 import org.w3c.dom.Text ; 39 40 43 public class DomUtil { 44 45 53 public static boolean getBooleanAttribute(Element element, String attributeName) { 54 String value = element.getAttribute(attributeName); 55 if (value == null) { 56 return false; 57 } else if (value.equals("true")) { 58 return true; 59 } else { 60 return false; 61 } 62 } 63 64 72 public static Element getChildElementByTagName(Element parentElement, String name) { 73 NodeList nodes = parentElement.getChildNodes(); 74 int length = nodes.getLength(); 75 for (int index = 0; index < length; ++index) { 76 if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE 77 && name.equals(nodes.item(index).getNodeName())) { 78 return (Element ) nodes.item(index); 79 } 80 } 81 return null; 82 } 83 84 93 public static Element getChildElementByTagNameNS(Element parentElement, String namespaceURI, String localName) { 94 NodeList nodes = parentElement.getChildNodes(); 95 int length = nodes.getLength(); 96 for (int index = 0; index < length; ++index) { 97 if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE 98 && namespaceURI.equals(nodes.item(index).getNamespaceURI()) 99 && localName.equals(nodes.item(index).getNodeName())) { 100 return (Element ) nodes.item(index); 101 } 102 } 103 return null; 104 } 105 106 114 public static Element [] getChildElementsByTagName(Element parentElement, String name) { 115 List children = new ArrayList (); 116 NodeList nodes = parentElement.getChildNodes(); 117 int length = nodes.getLength(); 118 for (int index = 0; index < length; ++index) { 119 if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE 120 && name.equals(nodes.item(index).getNodeName())) { 121 children.add(nodes.item(index)); 122 } 123 } 124 Element [] childElements = new Element [children.size()]; 125 return (Element []) children.toArray(childElements); 126 } 127 128 137 public static Element [] getChildElementsByTagNameNS(Element parentElement, String namespaceURI, String localName) { 138 List children = new ArrayList (); 139 NodeList nodes = parentElement.getChildNodes(); 140 int length = nodes.getLength(); 141 for (int index = 0; index < length; ++index) { 142 if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE 143 && namespaceURI.equals(nodes.item(index).getNamespaceURI()) 144 && localName.equals(nodes.item(index).getNodeName())) { 145 children.add(nodes.item(index)); 146 } 147 } 148 Element [] childElements = new Element [children.size()]; 149 return (Element []) children.toArray(childElements); 150 } 151 152 160 public static int getChildElementCountByTagName(Element parentElement, String name) { 161 NodeList nodes = parentElement.getChildNodes(); 162 int length = nodes.getLength(); 163 int count = 0; 164 for (int index = 0; index < length; ++index) { 165 if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE 166 && name.equals(nodes.item(index).getNodeName())) { 167 ++count; 168 } 169 } 170 return count; 171 } 172 173 182 public static int getChildElementCountByTagNameNS(Element parentElement, String namespaceURI, String localName) { 183 NodeList nodes = parentElement.getChildNodes(); 184 int length = nodes.getLength(); 185 int count = 0; 186 for (int index = 0; index < length; ++index) { 187 if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE 188 && namespaceURI.equals(nodes.item(index).getNamespaceURI()) 189 && localName.equals(nodes.item(index).getNodeName())) { 190 ++count; 191 } 192 } 193 return count; 194 } 195 196 201 public static String getElementText(Element element) { 202 NodeList children = element.getChildNodes(); 203 int childCount = children.getLength(); 204 for (int index = 0; index < childCount; ++index) { 205 if (children.item(index) instanceof Text ) { 206 Text text = (Text ) children.item(index); 207 return text.getData(); 208 } 209 } 210 return null; 211 } 212 213 219 public static void setElementText(Element element, String value) { 220 NodeList children = element.getChildNodes(); 221 int childCount = children.getLength(); 222 for (int index = 0; index < childCount; ++index) { 223 if (children.item(index) instanceof Text ) { 224 Text text = (Text ) children.item(index); 225 text.setData(value); 226 return; 227 } 228 } 229 Text text = element.getOwnerDocument().createTextNode(value); 230 element.appendChild(text); 231 } 232 233 234 private DomUtil() { } 235 } 236 | Popular Tags |