1 6 7 package org.jfox.ioc.util; 8 9 import java.io.BufferedInputStream ; 10 import java.io.IOException ; 11 import java.io.InputStream ; 12 import java.net.URL ; 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import javax.xml.parsers.DocumentBuilder ; 18 import javax.xml.parsers.DocumentBuilderFactory ; 19 20 import org.w3c.dom.Document ; 21 import org.w3c.dom.Element ; 22 import org.w3c.dom.Node ; 23 import org.w3c.dom.NodeList ; 24 import org.w3c.dom.Text ; 25 26 29 30 public class XMLUtils { 31 32 private XMLUtils() { 33 34 } 35 36 private final static DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 37 38 static { 39 dbf.setValidating(false); 40 dbf.setNamespaceAware(true); 41 dbf.setValidating(false); 42 43 }; 44 45 static ThreadLocal dbcache = new ThreadLocal () { 46 protected Object initialValue() { 47 try { 48 DocumentBuilder db = dbf.newDocumentBuilder(); 49 db.setEntityResolver(new DTDEntityResolver()); 50 return db; 51 } catch (Exception ex) { 52 ex.printStackTrace(); 53 return null; 54 } 55 } 56 }; 57 58 public static DocumentBuilder getDocumentBuilder() { 59 return (DocumentBuilder ) dbcache.get(); 60 } 61 68 public static Node getChildNodeOf(Node node, String tagName) { 69 for(Node temp = node.getFirstChild(); temp != null; temp = temp.getNextSibling()) 70 if(temp.getNodeType() == Node.ELEMENT_NODE 71 && tagName.equals(temp.getNodeName())) { 72 return temp; 73 } 74 return null; 75 } 76 77 public static String getChildNodeValueOf(Node node, String tagName) { 78 if(tagName.equals(node.getNodeName())) { 79 return getValueOf(node); 80 } 81 for(Node temp = node.getFirstChild(); temp != null; temp = temp.getNextSibling()) { 82 if(temp.getNodeType() == Node.ELEMENT_NODE && tagName.equals(temp.getNodeName())) { 83 return getValueOf(temp); 84 } 85 } 86 return null; 87 } 88 89 public static final String getValueOf(Node node) { 90 if(node == null) { 91 return null; 92 } 93 else if(node instanceof Text ) { 94 return node.getNodeValue().trim(); 95 } 96 else if(node instanceof Element ) { 97 ((Element ) node).normalize(); 98 Node temp = node.getFirstChild(); 99 if(temp != null && (temp instanceof Text )) 100 return temp.getNodeValue().trim(); 101 else 102 return ""; 103 } 104 else { 105 return node.getNodeValue().trim(); 106 } 107 } 108 109 public static final String getAtrributeValueOf(Node node, String attribute) { 110 Node _node = node.getAttributes().getNamedItem(attribute); 111 return getValueOf(_node); 112 } 113 114 public static Iterator getElementsByTagName(Element element, String tag) { 115 ArrayList children = new ArrayList (); 116 if(element != null && tag != null) { 117 NodeList nodes = element.getElementsByTagName(tag); 118 for(int i = 0; i < nodes.getLength(); i++) { 119 Node child = nodes.item(i); 120 children.add((Element ) child); 122 } 123 } 124 return children.iterator(); 125 } 126 127 public static Iterator getElementsByTagNames(Element element, String [] tags) { 128 List children = new ArrayList (); 129 if(element != null && tags != null) { 130 List tagList = Arrays.asList(tags); 131 NodeList nodes = element.getChildNodes(); 132 for(int i = 0; i < nodes.getLength(); i++) { 133 Node child = nodes.item(i); 134 if(child.getNodeType() == Node.ELEMENT_NODE 135 && tagList.contains(((Element ) child).getTagName())) { 136 children.add((Element ) child); 137 } 139 } 140 } 141 return children.iterator(); 142 } 143 144 150 151 public static Document getDocument(URL url) throws Exception { 152 InputStream is = null; 153 try { 154 is = new BufferedInputStream (url.openStream()); 155 return getDocumentBuilder().parse(is); 156 } 157 finally { 158 if(is != null) { 159 try { 160 is.close(); 161 } 162 catch(IOException e) { 163 164 } 165 } 166 } 167 } 168 169 public static void main(String [] args) { 170 171 } 172 } 173 174 | Popular Tags |