1 22 23 24 package com.mchange.v1.xml; 25 26 import java.util.*; 27 import org.xml.sax.*; 28 import org.w3c.dom.*; 29 import com.mchange.v1.util.DebugUtils; 30 31 public final class DomParseUtils 32 { 33 final static boolean DEBUG = true; 34 35 38 public static String allTextFromUniqueChild(Element elem, String childTagName) 39 throws DOMException 40 { return allTextFromUniqueChild( elem, childTagName, false ); } 41 42 45 public static String allTextFromUniqueChild(Element elem, String childTagName, boolean trim) 46 throws DOMException 47 { 48 Element uniqueChild = uniqueChildByTagName( elem, childTagName ); 49 if (uniqueChild == null) 50 return null; 51 else 52 return DomParseUtils.allTextFromElement( uniqueChild, trim ); 53 } 54 55 public static Element uniqueChild(Element elem, String childTagName) throws DOMException 56 { return uniqueChildByTagName( elem, childTagName); } 57 58 61 public static Element uniqueChildByTagName(Element elem, String childTagName) throws DOMException 62 { 63 NodeList nl = elem.getElementsByTagName(childTagName); 64 int len = nl.getLength(); 65 if (DEBUG) 66 DebugUtils.myAssert( len <= 1 , 67 "There is more than one (" + len + ") child with tag name: " + 68 childTagName + "!!!" ); 69 return (len == 1 ? (Element) nl.item( 0 ) : null); 70 } 71 72 public static String allText(Element elem) throws DOMException 73 { return allTextFromElement( elem ); } 74 75 public static String allText(Element elem, boolean trim) throws DOMException 76 { return allTextFromElement( elem, trim ); } 77 78 79 public static String allTextFromElement(Element elem) throws DOMException 80 { return allTextFromElement( elem, false); } 81 82 83 public static String allTextFromElement(Element elem, boolean trim) throws DOMException 84 { 85 StringBuffer textBuf = new StringBuffer (); 86 NodeList nl = elem.getChildNodes(); 87 for (int j = 0, len = nl.getLength(); j < len; ++j) 88 { 89 Node node = nl.item(j); 90 if (node instanceof Text) textBuf.append(node.getNodeValue()); 92 } 93 String out = textBuf.toString(); 94 return ( trim ? out.trim() : out ); 95 } 96 97 public static String [] allTextFromImmediateChildElements( Element parent, String tagName ) 98 throws DOMException 99 { return allTextFromImmediateChildElements( parent, tagName, false ); } 100 101 public static String [] allTextFromImmediateChildElements( Element parent, String tagName, boolean trim ) 102 throws DOMException 103 { 104 NodeList nl = immediateChildElementsByTagName( parent, tagName ); 105 int len = nl.getLength(); 106 String [] out = new String [ len ]; 107 for (int i = 0; i < len; ++i) 108 out[i] = allText( (Element) nl.item(i), trim ); 109 return out; 110 } 111 112 113 public static NodeList immediateChildElementsByTagName( Element parent, String tagName ) 114 throws DOMException 115 { return getImmediateChildElementsByTagName( parent, tagName ); } 116 117 120 public static NodeList getImmediateChildElementsByTagName( Element parent, String tagName ) 121 throws DOMException 122 { 123 final List nodes = new ArrayList(); 124 for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) 125 if (child instanceof Element && ((Element) child).getTagName().equals(tagName)) 126 nodes.add(child); 127 return new NodeList() 128 { 129 public int getLength() 130 { return nodes.size(); } 131 132 public Node item( int i ) 133 { return (Node) nodes.get( i ); } 134 }; 135 } 136 137 public static String allTextFromUniqueImmediateChild(Element elem, String childTagName) 138 throws DOMException 139 { 140 Element uniqueChild = uniqueImmediateChildByTagName( elem, childTagName ); 141 if (uniqueChild == null) 142 return null; 143 return DomParseUtils.allTextFromElement( uniqueChild ); 144 } 145 146 public static Element uniqueImmediateChild(Element elem, String childTagName) 147 throws DOMException 148 { return uniqueImmediateChildByTagName( elem, childTagName); } 149 150 153 public static Element uniqueImmediateChildByTagName(Element elem, String childTagName) 154 throws DOMException 155 { 156 NodeList nl = getImmediateChildElementsByTagName(elem, childTagName); 157 int len = nl.getLength(); 158 if (DEBUG) 159 DebugUtils.myAssert( len <= 1 , 160 "There is more than one (" + len + ") child with tag name: " + 161 childTagName + "!!!" ); 162 return (len == 1 ? (Element) nl.item( 0 ) : null); 163 } 164 165 168 public static String attrValFromElement(Element element, String attrName) 169 throws DOMException 170 { 171 Attr attr = element.getAttributeNode( attrName ); 172 return (attr == null ? null : attr.getValue()); 173 } 174 175 private DomParseUtils() 176 {} 177 } 178 179 180 | Popular Tags |