1 3 package jodd.util; 4 5 import org.w3c.dom.NamedNodeMap ; 6 import org.w3c.dom.Node ; 7 import org.w3c.dom.NodeList ; 8 import org.w3c.dom.Element ; 9 10 import java.util.ArrayList ; 11 import java.util.HashMap ; 12 import java.util.List ; 13 import java.util.Map ; 14 15 18 public class XmlUtil { 19 20 21 23 26 public static Map getAllAttributes(Node node) { 27 HashMap attrs = new HashMap (); 28 NamedNodeMap nmm = node.getAttributes(); 29 for (int j = 0; j < nmm.getLength(); j++) { 30 Node attribute = nmm.item(j); 31 if (attribute.getNodeType() != Node.ATTRIBUTE_NODE) { 32 continue; 33 } 34 attrs.put(attribute.getNodeName(), attribute.getNodeValue()); 35 } 36 return attrs; 37 } 38 39 44 public static String getAttributeValue(Node node, String attrName) { 45 NamedNodeMap nmm = node.getAttributes(); 46 for (int j = 0; j < nmm.getLength(); j++) { 47 Node attribute = nmm.item(j); 48 if (attribute.getNodeType() != Node.ATTRIBUTE_NODE) { 49 continue; 50 } 51 String nodeName = attribute.getNodeName(); 52 if (nodeName.equals(attrName)) { 53 return attribute.getNodeValue(); 54 } 55 } 56 return null; 57 } 58 59 62 public static String getAttributeValue(Element element, String name) { 63 String value = element.getAttribute(name); 64 if (value.length() == 0) { 65 value = null; 66 } 67 return value; 68 } 69 70 71 73 76 public static List filterNodeList(NodeList nodeList, short keepNodeType) { 77 return filterNodeList(nodeList, keepNodeType, null); 78 } 79 80 83 public static List filterNodeList(NodeList nodeList, short keepNodeType, String nodeName) { 84 List nodes = new ArrayList (); 85 for (int k = 0; k < nodeList.getLength(); k++) { 86 Node node = nodeList.item(k); 87 if (node.getNodeType() != keepNodeType) { 88 continue; 89 } 90 if (nodeName != null && (node.getNodeName().equals(nodeName) == false)) { 91 continue; 92 } 93 nodes.add(node); 94 } 95 return nodes; 96 } 97 98 101 public static List filterNodeListElements(NodeList nodeList) { 102 return filterNodeListElements(nodeList, null); 103 } 104 105 108 public static List filterNodeListElements(NodeList nodeList, String nodeName) { 109 List nodes = new ArrayList (); 110 for (int k = 0; k < nodeList.getLength(); k++) { 111 Node node = nodeList.item(k); 112 if (node.getNodeType() != Node.ELEMENT_NODE) { 113 continue; 114 } 115 if (nodeName != null && (node.getNodeName().equals(nodeName) == false)) { 116 continue; 117 } 118 nodes.add(node); 119 } 120 return nodes; 121 } 122 123 124 127 public static List getChildElements(Node node) { 128 return getChildElements(node, null); 129 } 130 131 132 135 public static List getChildElements(Node node, String nodeName) { 136 NodeList childs = node.getChildNodes(); 137 return filterNodeListElements(childs, nodeName); 138 } 139 140 142 143 146 public static String getFirstChildTextNodeValue(Node node) { 147 NodeList children = node.getChildNodes(); 148 int len = children.getLength(); 149 for (int i = 0; i < len; i++) { 150 Node n = children.item(i); 151 if (n.getNodeType() == Node.TEXT_NODE) { 152 return n.getNodeValue(); 153 } 154 } 155 return null; 156 } 157 158 161 public static String getChildTextNodeValue(Node node) { 162 if (node.getChildNodes().getLength() != 1) { 163 return null; 164 } 165 Node item0 = node.getChildNodes().item(0); 166 if (item0.getNodeType() != Node.TEXT_NODE) { 167 return null; 168 } 169 return item0.getNodeValue(); 170 } 171 172 173 174 } 175 | Popular Tags |