List of usage examples for org.w3c.dom Node getNodeType
public short getNodeType();
From source file:Main.java
/** * Node is element whose namespace is "namespace" and localname is "localname"? * /* www. jav a2 s . com*/ * @param node XML node. * @param namespace Namespace * @param localname Local name * @return Node is element whose namespace is "namespace" and localname is "localname"? */ public static boolean nodeIsElementOf(Node node, String namespace, String localname) { if (node.getNodeType() != Node.ELEMENT_NODE) { return false; } String ns = node.getNamespaceURI(); if (((ns != null) && (namespace != null) && (ns.equals(namespace))) || ((ns == null) && (namespace == null))) { } else { return false; } String ln = node.getLocalName(); if (!ln.equals(localname)) { return false; } return true; }
From source file:Main.java
static boolean canBeMerged(Node node1, Node node2, String requiredTagName) { if (node1.getNodeType() != Node.ELEMENT_NODE || node2.getNodeType() != Node.ELEMENT_NODE) return false; Element element1 = (Element) node1; Element element2 = (Element) node2; if (!equals(requiredTagName, element1.getTagName()) || !equals(requiredTagName, element2.getTagName())) return false; NamedNodeMap attributes1 = element1.getAttributes(); NamedNodeMap attributes2 = element2.getAttributes(); if (attributes1.getLength() != attributes2.getLength()) return false; for (int i = 0; i < attributes1.getLength(); i++) { final Attr attr1 = (Attr) attributes1.item(i); final Attr attr2; if (isNotEmpty(attr1.getNamespaceURI())) attr2 = (Attr) attributes2.getNamedItemNS(attr1.getNamespaceURI(), attr1.getLocalName()); else// w ww. j a v a 2 s .c om attr2 = (Attr) attributes2.getNamedItem(attr1.getName()); if (attr2 == null || !equals(attr1.getTextContent(), attr2.getTextContent())) return false; } return true; }
From source file:Main.java
/** * Recursively strips the namespace information from a node. * @param node the starting node./*ww w. j av a 2 s . c om*/ * @param document host document * @return clean node */ public static Node removeNamespaceRecursive(final Node node, final Document document) { Node newNode = null; if (node.getNodeType() == Node.ELEMENT_NODE) { newNode = document.renameNode(node, null, removeNsPrefix(node.getNodeName())); } final NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); ++i) { removeNamespaceRecursive(list.item(i), document); } return newNode; }
From source file:Main.java
/** * // www . j ava2 s. c om * @param currentNode * @param tagName * @param attributeValue * @return */ public static String getTextContentByElementNameANDAttributeValue(Node currentNode, String tagName, String attributeValue) { String result = ""; NodeList childNodeList = currentNode.getChildNodes(); for (int i = 0; i < childNodeList.getLength(); i++) { Node childNode = childNodeList.item(i); switch (childNode.getNodeType()) { case Node.DOCUMENT_NODE: break; case Node.ELEMENT_NODE: Element childElement = (Element) childNodeList.item(i); // logger.debug("childElement name : " + childElement.getTagName()); if (childElement != null && childElement.getNodeName().equals(tagName)) { NamedNodeMap attributes = childElement.getAttributes(); for (int j = 0; j < attributes.getLength(); j++) { Node current = attributes.item(j); if (current.getNodeName().equals("type") && current.getNodeValue().equals(attributeValue)) { result = childElement.getTextContent(); break; } } } case Node.TEXT_NODE: // logger.debug("textElement name : " + currentNode.getNodeValue()); break; case Node.COMMENT_NODE: break; case Node.PROCESSING_INSTRUCTION_NODE: break; case Node.ENTITY_REFERENCE_NODE: break; case Node.DOCUMENT_TYPE_NODE: break; } } return result; }
From source file:Main.java
public static String getText(Node node) { StringBuffer result = new StringBuffer(); if (!node.hasChildNodes()) return ""; NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) {/*from ww w . ja v a2 s.c o m*/ Node subnode = list.item(i); if (subnode.getNodeType() == Node.TEXT_NODE) { result.append(subnode.getNodeValue()); } else if (subnode.getNodeType() == Node.CDATA_SECTION_NODE) { result.append(subnode.getNodeValue()); } else if (subnode.getNodeType() == Node.ENTITY_REFERENCE_NODE) { // Recurse into the subtree for text // (and ignore comments) result.append(getText(subnode)); } } return result.toString(); }
From source file:Main.java
public static String getElementBody(Element element) { org.w3c.dom.Node valueNode = element.getFirstChild(); if (valueNode == null) { return null; }//from w w w . java2 s .co m if (valueNode.getNodeType() == org.w3c.dom.Node.TEXT_NODE) { return valueNode.getNodeValue(); } else { return null; } }
From source file:Main.java
/** * This method will find all the parameters under this <code>paramsElement</code> and return them as * Map<String, String>. For example, * <pre>/*ww w. java 2s.c o m*/ * <result ... > * <param name="param1">value1</param> * <param name="param2">value2</param> * <param name="param3">value3</param> * </result> * </pre> * will returns a Map<String, String> with the following key, value pairs :- * <ul> * <li>param1 - value1</li> * <li>param2 - value2</li> * <li>param3 - value3</li> * </ul> * * @param paramsElement * @return */ public static Map getParams(Element paramsElement) { LinkedHashMap params = new LinkedHashMap(); if (paramsElement == null) { return params; } NodeList childNodes = paramsElement.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node childNode = childNodes.item(i); if ((childNode.getNodeType() == Node.ELEMENT_NODE) && "param".equals(childNode.getNodeName())) { Element paramElement = (Element) childNode; String paramName = paramElement.getAttribute("name"); String val = getContent(paramElement); if (val.length() > 0) { params.put(paramName, val); } } } return params; }
From source file:Main.java
static String getTextValue(Node node) throws SAXException { Node textNode = node.getFirstChild(); if (textNode == null) return ""; if (textNode.getNodeType() != Node.TEXT_NODE) throw new SAXException("No text value found for <" + node.getNodeName() + "> node"); return textNode.getNodeValue(); }
From source file:Main.java
public static Map<String, Object> convertNodeToMap(Node node) { NodeList nodeList = node.getChildNodes(); Map<String, Object> map = new HashMap<String, Object>(nodeList.getLength()); for (int i = 0; i < nodeList.getLength(); i++) { Node nodec = nodeList.item(i); String key = nodec.getNodeName(); Object value = null;/*ww w. j av a 2 s.com*/ if (nodec.hasChildNodes()) { NodeList nodeListc = nodec.getChildNodes(); if (nodeListc.getLength() == 1) { Node noded = nodeListc.item(0); short type = noded.getNodeType(); if (type == 3 || type == 4) { value = noded.getNodeValue(); } if (noded.getNodeType() == 1) { value = convertNodeToMap(nodec); } } else { value = convertNodeToMap(nodec); } } map.put(key, value); } return map; }