List of usage examples for org.w3c.dom Node getNodeName
public String getNodeName();
From source file:Main.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 ww w.j a v a 2 s . com if (name.startsWith("#")) { return ""; } StringBuilder sb = new StringBuilder(); 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)) { if ((textContent = n.getTextContent()) != null && !"".equals(textContent)) { sb.append(textContent).append("</").append(name).append('>'); } else { sb.append("/>"); } } else { sb.append('>'); boolean hasValidChildren = false; for (int i = 0; i < children.getLength(); i++) { String childToString = elementToString(children.item(i)); if (!"".equals(childToString)) { sb.append('\n').append(childToString); hasValidChildren = true; } } if (hasValidChildren) { sb.append('\n'); } // if (!hasValidChildren && ((textContent = XMLUtil.getTextContent(n)) != null)) { if (!hasValidChildren && ((textContent = n.getTextContent()) != null)) { sb.append(textContent); } sb.append("</").append(name).append('>'); } return sb.toString(); }
From source file:Main.java
/*** * Get the name of the node without namespace * // ww w. j a v a2s .c o m * @param node * @return */ public static String getNodeName(final Node node) { if (node == null) { return null; } String nodeName = node.getNodeName(); // Remove namespace prefix if present int indexOfColon; if ((nodeName != null) && (nodeName.length() > 0 // Node name is not // empty ) && ((indexOfColon = nodeName.indexOf(':')) >= 0) // there is a colon in the nodename && (indexOfColon < (nodeName.length() - 1)) // colon is not at the end of the name ) { nodeName = nodeName.substring(nodeName.indexOf(':') + 1); } return nodeName; }
From source file:Main.java
public static void visitRecursively(Node node, BufferedWriter bw) throws Exception { NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { // get child node Node childNode = list.item(i); if (childNode.getNodeType() == Node.TEXT_NODE) { System.out.println("Found Node: " + childNode.getNodeName() + " - with value: " + childNode.getNodeValue() + " Node type:" + childNode.getNodeType()); String nodeValue = childNode.getNodeValue(); nodeValue = nodeValue.replace("\n", "").replaceAll("\\s", ""); if (!nodeValue.isEmpty()) { System.out.println(nodeValue); bw.write(nodeValue);//from ww w . ja v a 2 s .c o m bw.newLine(); } } visitRecursively(childNode, bw); } }
From source file:Main.java
public static Element getFirstChildElementIgnoreCase(Element elm, String name) { if (elm == null) return null; NodeList nl = elm.getChildNodes(); for (int c = 0; c < nl.getLength(); c++) { Node node = nl.item(c); if (node.getNodeType() == Node.ELEMENT_NODE && (name == null || node.getNodeName().equalsIgnoreCase(name))) return (Element) node; }/* w w w . j ava 2 s.com*/ return null; }
From source file:Main.java
public static Element getElement(Document document, String[] path, String value, String translate, String module) {/*from w ww .ja v a 2s . co m*/ Node node = document; NodeList list; for (int i = 0; i < path.length; ++i) { list = node.getChildNodes(); boolean found = false; for (int j = 0; j < list.getLength(); ++j) { Node testNode = list.item(j); if (testNode.getNodeName().equals(path[i])) { found = true; node = testNode; break; } } if (found == false) { Element element = document.createElement(path[i]); node.appendChild(element); node = element; } } if (value != null) { Text text = document.createTextNode(value); list = node.getChildNodes(); boolean found = false; for (int j = 0; j < list.getLength(); ++j) { Node testNode = list.item(j); if (testNode instanceof Text) { node.replaceChild(text, testNode); found = true; break; } } if (!found) node.appendChild(text); } if (node instanceof Element) { Element element = (Element) node; if (translate != null && element.getAttribute("translate").equals("")) { element.setAttribute("translate", translate); } if (module != null && element.getAttribute("module").equals("")) { element.setAttribute("module", module); } } return (Element) node; }
From source file:MainClass.java
static void listNodes(Node node, String indent) { String nodeName = node.getNodeName(); System.out.println(indent + " Node: " + nodeName); short type = node.getNodeType(); System.out.println(indent + " Node Type: " + nodeType(type)); if (type == TEXT_NODE) { System.out.println(indent + " Content is: " + ((Text) node).getWholeText()); } else if (node.hasAttributes()) { System.out.println(indent + " Element Attributes are:"); NamedNodeMap attrs = node.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Attr attribute = (Attr) attrs.item(i); System.out.println(indent + " " + attribute.getName() + " = " + attribute.getValue()); }/*from ww w . ja v a 2 s . c o m*/ } NodeList list = node.getChildNodes(); if (list.getLength() > 0) { System.out.println(indent + " Child Nodes of " + nodeName + " are:"); for (int i = 0; i < list.getLength(); i++) { listNodes(list.item(i), indent + " "); } } }
From source file:Main.java
/** * Get the first child Element of the supplied node that matches a given tag name. * * @param node The DOM Node./* www . j a v a2s. c o m*/ * @param name The name of the child node to search for. * @return The first child element with the matching tag name. */ public static Element getFirstChildElementByName(Node node, String name) { NodeList children = node.getChildNodes(); int childCount = children.getLength(); for (int i = 0; i < childCount; i++) { Node child = children.item(i); if (child != null && child.getNodeType() == Node.ELEMENT_NODE && child.getNodeName() != null && child.getNodeName().equals(name)) { return (Element) child; } } return null; }
From source file:Main.java
/** * Remove named nodes of the specified nodeType from the specified node. * //from w w w . j a v a2s . c o m * @param node the node to be cleaned. * @param nodeType the type of nodes to be removed. * @param name the name of nodes to be removed. */ public static void removeAll(final Node node, final short nodeType, final String name) { if (node.getNodeType() == nodeType && (name == null || node.getNodeName().equals(name))) { node.getParentNode().removeChild(node); } else { // Visit the children final NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { removeAll(list.item(i), nodeType, name); } } }
From source file:Main.java
static public List<Node> getAttributeNodeList(Element element, Pattern name) { NamedNodeMap nodes = element.getAttributes(); List<Node> attributes = new ArrayList<>(); int i, size = nodes.getLength(); Node node; for (i = 0; i < size; i++) { node = nodes.item(i);// w w w . j av a2 s . co m if (name.matcher(node.getNodeName()).find()) attributes.add(node); } return attributes; }
From source file:Main.java
/** * Get the first node with a given name and returns it * * @param root the node that contains the children to search within * @param name the name of the node//from w w w . ja v a2 s .c o m * @return a node with given or null if not found */ public static Node find(Node root, String name) { final NodeList list = root.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { final Node subnode = list.item(i); if (subnode.getNodeType() == Node.ELEMENT_NODE) { if (subnode.getNodeName().equals(name)) { return subnode; } } } return null; }