List of usage examples for org.w3c.dom Node ELEMENT_NODE
short ELEMENT_NODE
To view the source code for org.w3c.dom Node ELEMENT_NODE.
Click Source Link
Element
. From source file:Main.java
public static ArrayList<Element> getElements(String elementName, Element element) { ArrayList<Element> result = new ArrayList<Element>(); NodeList list = element.getElementsByTagName(elementName); for (int i = 0; i < list.getLength(); i++) { Node nNode = list.item(i); if (nNode.getNodeType() == Node.ELEMENT_NODE) { result.add((Element) nNode); }//from w w w . ja v a 2s.c om } return result; }
From source file:Main.java
/** * Check whether a given XML <code>Node</code> is * an element node.//from w w w .j a va 2 s .c o m * * Sun should've implemented <code>Node.hasType(int)</code>.... * * @param n * @return */ public static final boolean isElementNode(Node n) { return n != null && n.getNodeType() == Node.ELEMENT_NODE; }
From source file:Main.java
public static Element getElementByTagName(Node element, String name) { NodeList elements = element.getChildNodes(); if (elements != null) { for (int i = 0; i < elements.getLength(); i++) { if (elements.item(i).getNodeType() == Node.ELEMENT_NODE) { Element currentElement = (Element) elements.item(i); if (currentElement.getNodeName().equals(name)) { return currentElement; }// w w w . j a v a2s. co m } } } return null; }
From source file:Main.java
/** * Recursively removes all text nodes from a DOM element. * /*from www.j av a 2 s.c o m*/ * @param element * The element */ public static void removeTextNodes(Element element) { Node nextNode = element.getFirstChild(); for (Node child = element.getFirstChild(); nextNode != null;) { child = nextNode; nextNode = child.getNextSibling(); if (child.getNodeType() == Node.TEXT_NODE) element.removeChild(child); else if (child.getNodeType() == Node.ELEMENT_NODE) removeTextNodes((Element) child); } }
From source file:Main.java
/** * Identify variables in attribute values and CDATA contents and replace * with their values as defined in the variableDefs map. Variables without * definitions are left alone.//from ww w . j ava 2s . co m * * @param node the node to do variable replacement in * @param variableDefs map from variable names to values. */ public static void replaceVariables(final Node node, Map<String, String> variableDefs) { switch (node.getNodeType()) { case Node.ELEMENT_NODE: final Element element = (Element) node; final NamedNodeMap atts = element.getAttributes(); for (int i = 0; i < atts.getLength(); i++) { final Attr attr = (Attr) atts.item(i); attr.setNodeValue(replaceVariablesInString(attr.getValue(), variableDefs)); } break; case Node.CDATA_SECTION_NODE: String content = node.getTextContent(); node.setNodeValue(replaceVariablesInString(content, variableDefs)); break; default: break; } // process children final NodeList children = node.getChildNodes(); for (int childIndex = 0; childIndex < children.getLength(); childIndex++) replaceVariables(children.item(childIndex), variableDefs); }
From source file:Main.java
public static Element[] getChildren(Element parent, String name) { ArrayList<Element> al = new ArrayList<Element>(); NodeList nl = parent.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node n = nl.item(i);//from w w w . j a v a2 s . c om if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(name)) { al.add((Element) n); } } return al.toArray(new Element[al.size()]); }
From source file:Main.java
public static String format(Node n, int indentLevel) { String indent = ""; for (int i = 0; i < indentLevel; i++) { indent += "\t"; }// w w w . ja v a 2 s . c o m StringBuilder result = new StringBuilder(); result.append(indent); if (n.getNodeType() == Node.ELEMENT_NODE) { result.append("<"); result.append(n.getNodeName()); NamedNodeMap attrs = n.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i); result.append(" "); result.append(attr.getNodeName()); result.append("=\""); result.append(attr.getNodeValue()); result.append("\""); } result.append(">\n"); } if (n.getNodeType() == Node.TEXT_NODE) { String str = n.getNodeValue(); result.append(str + "\n"); } NodeList childNodes = n.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node n2 = childNodes.item(i); if (isEmptyTextNode(n2)) { continue; } result.append(format(n2, indentLevel + 1)); } if (n.getNodeType() == Node.ELEMENT_NODE) { result.append(indent); result.append("</"); result.append(n.getNodeName()); result.append(">\n"); } return result.toString(); }
From source file:Main.java
public static List<Element> getChildElements(Element element) { List<Element> elements = new LinkedList<Element>(); NodeList children = element.getChildNodes(); if (children == null) { return elements; }// w ww.j a va 2s. c om for (int i = 0, j = children.getLength(); i < j; i++) { Node child = children.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { elements.add((Element) child); } } return elements; }
From source file:Main.java
public static String getXMLValueByName(String xmlFilePath, String name) { String returnValue = "No Value!"; DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); try {//from w ww . ja v a2 s. c o m DocumentBuilder builder = builderFactory.newDocumentBuilder(); InputStream is = new FileInputStream(xmlFilePath); Document doc = builder.parse(is); Element root = doc.getDocumentElement(); NodeList nodes = root.getChildNodes(); if (nodes != null) { for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { String test = node.getNodeName(); if (test.equals(name)) { returnValue = node.getTextContent().trim(); } } } } } catch (Exception e) { } return returnValue; }
From source file:Main.java
/** * Returns a List of all descendant Element nodes having the specified * [namespace name] property. The elements are listed in document order. * //w ww . j a v a 2 s . co m * @param node * The node to search from. * @param namespaceURI * An absolute URI denoting a namespace name. * @return A List containing elements in the specified namespace; the list * is empty if there are no elements in the namespace. */ public static List<Element> getElementsByNamespaceURI(Node node, String namespaceURI) { List<Element> list = new ArrayList<Element>(); NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child.getNodeType() != Node.ELEMENT_NODE) continue; if (child.getNamespaceURI().equals(namespaceURI)) list.add((Element) child); } return list; }