List of usage examples for org.w3c.dom Node getNodeName
public String getNodeName();
From source file:Main.java
/** * Check whether a name of given node is equal to a given name. Strips * namespace (if any). Case-sensitive./*from w w w . j av a2s.c o m*/ */ private static boolean nodeNameEqualTo(Node node, String target) { if (node == null || target == null) return false; String name = node.getNodeName(); // If target contains namespace, require exact match if (target.indexOf(':') < 0) { int index = name.indexOf(':'); if (index >= 0) name = name.substring(index + 1); // Strip namespace } return name.equals(target); }
From source file:Main.java
/** * Find all attribute in a node with specific prefix. * * @param node the note that contains the attributes * @param prefix the prefix to search for * @param removePrefix true: removes the prefix from attribute name (name is the key of resulting map) * @return the values or a empty map// w w w .jav a 2 s. c om */ public static HashMap<String, String> getAttributes(Node node, String prefix, boolean removePrefix) { final HashMap<String, String> result = new HashMap<>(); final NamedNodeMap attributes = node.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { final Node att = attributes.item(i); String name = att.getNodeName(); if (name.startsWith(prefix)) { if (removePrefix) { name = name.substring(prefix.length()); } result.put(name, att.getNodeValue()); } } return result; }
From source file:Main.java
public static void parsePseudoHTML(Node e, StringBuffer html) { String nodeName = e.getNodeName(); boolean toAppendTag = !(e instanceof Text); boolean isBR = nodeName.equals("BR"); if (toAppendTag & !isBR) { html.append('<'); html.append(nodeName);//from w w w. ja v a2s . co m html.append('>'); } if (!e.hasChildNodes() && !isBR) { html.append(e.getNodeValue()); } else { NodeList nodes = e.getChildNodes(); int children = nodes.getLength(); for (int i = 0; i < children; i++) parsePseudoHTML(nodes.item(i), html); } if (toAppendTag) { if (isBR) { html.append("<BR>"); } else { html.append("</"); html.append(nodeName); html.append(">\n"); } } }
From source file:Main.java
public static String argumentTagToCmd(Element jobElement) { // NodeList nl = jobElement.getChildNodes(); // for(int i=0;i<nl.getLength();i++) // {/*ww w . j a v a2 s.co m*/ // System.out.println(nl.item(i).getTextContent()); // } // System.exit(1); // Node n = jobElement.getElementsByTagName("argument").item(0); String taskName = jobElement.getAttribute("name"); // String nodeString = nodeToString(n); // nodeString = nodeString.replace("<"+n.getNodeName()+">", ""); // nodeString = nodeString.replace("</"+n.getNodeName()+">", ""); // nodeString = nodeString.trim(); // String[] lines = nodeString.split("\n"); StringBuilder cmd = new StringBuilder(taskName).append(";"); NodeList argList = jobElement.getElementsByTagName("argument").item(0).getChildNodes(); for (int i = 0; i < argList.getLength(); i++) { Node c = argList.item(i); String cStr; if (c.getNodeName().equals("file")) { Element ec = (Element) c; cStr = (ec.getAttribute("name")).trim(); if (!cStr.isEmpty()) { cmd.append(cStr).append(";"); } } else { cStr = (c.getTextContent().trim()); String[] cStrs = cStr.split("\\s+"); for (String cs : cStrs) { cs = cs.trim(); if (!cs.isEmpty()) { cmd.append(cs).append(";"); } } } } cmd.replace(cmd.length() - 1, cmd.length(), ""); return cmd.toString(); }
From source file:Utils.java
public static String elementToString(Node n) { String name = n.getNodeName(); short type = n.getNodeType(); if (Node.CDATA_SECTION_NODE == type) { return "<![CDATA[" + n.getNodeValue() + "]]>"; }/*from w w w . j a va 2s . c o m*/ if (name.startsWith("#")) { return ""; } StringBuffer sb = new StringBuffer(); sb.append('<').append(name); NamedNodeMap attrs = n.getAttributes(); if (attrs != null) { for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i); sb.append(' ').append(attr.getNodeName()).append("=\"").append(attr.getNodeValue()).append("\""); } } String textContent = null; NodeList children = n.getChildNodes(); if (children.getLength() == 0) { if ((textContent = XMLUtil.getTextContent(n)) != null && !"".equals(textContent)) { sb.append(textContent).append("</").append(name).append('>'); ; } else { sb.append("/>").append('\n'); } } else { sb.append('>').append('\n'); boolean hasValidChildren = false; for (int i = 0; i < children.getLength(); i++) { String childToString = elementToString(children.item(i)); if (!"".equals(childToString)) { sb.append(childToString); hasValidChildren = true; } } if (!hasValidChildren && ((textContent = XMLUtil.getTextContent(n)) != null)) { sb.append(textContent); } sb.append("</").append(name).append('>'); } return sb.toString(); }
From source file:Main.java
/** * Sets the contents of the child elements that match the type * @param parent The element whose children will be updated * @param type The name of the children elements to updated * @param firstOnly True if only the first child of type is to be updated *//*ww w . j ava 2s . co m*/ public static void setChildrenContent(final Node parent, final String type, final String content, final boolean firstOnly) { final NodeList children = parent.getChildNodes(); for (int childIndex = 0; childIndex < children.getLength(); ++childIndex) { final Node child = children.item(childIndex); if (child.getNodeName().equals(type)) { child.setTextContent(content); if (firstOnly) { break; } } } }
From source file:Main.java
/** * //from www . j a v a2 s.co m * @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
/** * @param n1 first Node to test// w ww . j av a 2 s .co m * @param n2 second Node to test * @return true if a deep compare show the same children and attributes in the same order */ public static boolean equals(Node n1, Node n2) { // compare type if (!n1.getNodeName().equals(n2.getNodeName())) return false; // compare attributes NamedNodeMap nnm1 = n1.getAttributes(); NamedNodeMap nnm2 = n2.getAttributes(); if (nnm1.getLength() != nnm2.getLength()) return false; for (int i = 0; i < nnm1.getLength(); i++) { Node attr1 = nnm1.item(i); if (!getAttribute(n1, attr1.getNodeName()).equals(getAttribute(n2, attr1.getNodeName()))) return false; } // compare children Node c1 = n1.getFirstChild(); Node c2 = n2.getFirstChild(); for (;;) { while ((c1 != null) && c1.getNodeName().startsWith("#")) c1 = c1.getNextSibling(); while ((c2 != null) && c2.getNodeName().startsWith("#")) c2 = c2.getNextSibling(); if ((c1 == null) && (c2 == null)) break; if ((c1 == null) || (c2 == null)) return false; if (!equals(c1, c2)) return false; c1 = c1.getNextSibling(); c2 = c2.getNextSibling(); } return true; }
From source file:Main.java
public static String getPrefix(Node node) { String prefix = node.getPrefix(); if (prefix == null) { prefix = node.getNodeName(); // If the document is not namespace aware, we must split the attribute name // with ':' character. int i = prefix.indexOf(':'); if (i != -1) { prefix = prefix.substring(0, i); }// ww w. j a v a2 s . com } return prefix; }
From source file:Main.java
public static String getChildNodeValue(String nodeName, Node parent) { if (parent == null) { return null; }//from ww w. jav a 2s. c o m NodeList childNodes = parent.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node node = childNodes.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { if (node.getNodeName().equals(nodeName)) { Node firstChild = node.getFirstChild(); if ((firstChild != null) && (firstChild.getNodeType() == Node.TEXT_NODE)) { return firstChild.getNodeValue(); } else { return null; } } } } return null; }