1 16 17 package samples.addr; 18 19 import org.w3c.dom.Attr ; 20 import org.w3c.dom.CharacterData ; 21 import org.w3c.dom.Element ; 22 import org.w3c.dom.Node ; 23 import org.w3c.dom.NodeList ; 24 25 29 public class DOMUtils { 30 33 private static String NS_URI_XMLNS = "http://www.w3.org/2000/xmlns/"; 34 35 44 static public String getAttribute (Element el, String attrName) { 45 String sRet = null; 46 Attr attr = el.getAttributeNode(attrName); 47 48 if (attr != null) { 49 sRet = attr.getValue(); 50 } 51 return sRet; 52 } 53 54 64 static public String getAttributeNS (Element el, 65 String namespaceURI, 66 String localPart) { 67 String sRet = null; 68 Attr attr = el.getAttributeNodeNS (namespaceURI, localPart); 69 70 if (attr != null) { 71 sRet = attr.getValue (); 72 } 73 74 return sRet; 75 } 76 77 85 static public String getChildCharacterData (Element parentEl) { 86 if (parentEl == null) { 87 return null; 88 } 89 Node tempNode = parentEl.getFirstChild(); 90 StringBuffer strBuf = new StringBuffer (); 91 CharacterData charData; 92 93 while (tempNode != null) { 94 switch (tempNode.getNodeType()) { 95 case Node.TEXT_NODE : 96 case Node.CDATA_SECTION_NODE : charData = (CharacterData )tempNode; 97 strBuf.append(charData.getData()); 98 break; 99 } 100 tempNode = tempNode.getNextSibling(); 101 } 102 return strBuf.toString(); 103 } 104 105 112 public static Element getFirstChildElement (Element elem) { 113 for (Node n = elem.getFirstChild (); n != null; n = n.getNextSibling ()) { 114 if (n.getNodeType () == Node.ELEMENT_NODE) { 115 return (Element ) n; 116 } 117 } 118 return null; 119 } 120 121 128 public static Element getNextSiblingElement (Element elem) { 129 for (Node n = elem.getNextSibling (); n != null; n = n.getNextSibling ()) { 130 if (n.getNodeType () == Node.ELEMENT_NODE) { 131 return (Element ) n; 132 } 133 } 134 return null; 135 } 136 137 147 public static Element findChildElementWithAttribute (Element elem, 148 String attrName, 149 String attrValue) { 150 for (Node n = elem.getFirstChild (); n != null; n = n.getNextSibling ()) { 151 if (n.getNodeType () == Node.ELEMENT_NODE) { 152 if (attrValue.equals (DOMUtils.getAttribute ((Element ) n, attrName))) { 153 return (Element ) n; 154 } 155 } 156 } 157 return null; 158 } 159 160 167 public static int countKids (Element elem, short nodeType) { 168 int nkids = 0; 169 for (Node n = elem.getFirstChild (); n != null; n = n.getNextSibling ()) { 170 if (n.getNodeType () == nodeType) { 171 nkids++; 172 } 173 } 174 return nkids; 175 } 176 177 189 public static String getNamespaceURIFromPrefix (Node context, 190 String prefix) { 191 short nodeType = context.getNodeType (); 192 Node tempNode = null; 193 194 switch (nodeType) 195 { 196 case Node.ATTRIBUTE_NODE : 197 { 198 tempNode = ((Attr ) context).getOwnerElement (); 199 break; 200 } 201 case Node.ELEMENT_NODE : 202 { 203 tempNode = context; 204 break; 205 } 206 default : 207 { 208 tempNode = context.getParentNode (); 209 break; 210 } 211 } 212 213 while (tempNode != null && tempNode.getNodeType () == Node.ELEMENT_NODE) 214 { 215 Element tempEl = (Element ) tempNode; 216 String namespaceURI = (prefix == null) 217 ? getAttribute (tempEl, "xmlns") 218 : getAttributeNS (tempEl, NS_URI_XMLNS, prefix); 219 220 if (namespaceURI != null) 221 { 222 return namespaceURI; 223 } 224 else 225 { 226 tempNode = tempEl.getParentNode (); 227 } 228 } 229 230 return null; 231 } 232 233 public static Element getElementByID(Element el, String id) 234 { 235 if (el == null) 236 return null; 237 String thisId = el.getAttribute("id"); 238 if (id.equals(thisId)) 239 return el; 240 241 NodeList list = el.getChildNodes(); 242 for (int i = 0; i < list.getLength(); i++) { 243 Node node = list.item(i); 244 if (node instanceof Element ) { 245 Element ret = getElementByID((Element )node, id); 246 if (ret != null) 247 return ret; 248 } 249 } 250 251 return null; 252 } 253 } 254 | Popular Tags |