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
/** * Determine whether a single child is available of a particular type. * @return true if the specified child element is present * @throws Exception if the child is present multiple times. *//* ww w .j ava2 s . c o m*/ public static boolean hasChild(Element element, String child) throws Exception { NodeList nodes = element.getChildNodes(); Element ret = null; for (int i = 0; i < nodes.getLength(); i++) { Node childNode = nodes.item(i); if (childNode.getNodeName().equals(child) && childNode.getNodeType() == Node.ELEMENT_NODE) { if (ret != null) { throw new Exception("Child element '" + child + "' present multiple times"); } else { ret = (Element) childNode; } } } return ret != null; }
From source file:Main.java
/** * Format generated xml doc by indentation * * @param node For java, rather than GWT * @param indent//from ww w . j a v a 2 s . co m * @return */ public static String format(Node node, String indent) { StringBuilder formatted = new StringBuilder(); if (node.getNodeType() == Node.ELEMENT_NODE) { StringBuilder attributes = new StringBuilder(); for (int k = 0; k < node.getAttributes().getLength(); k++) { attributes.append(" "); attributes.append(node.getAttributes().item(k).getNodeName()); attributes.append("=\""); attributes.append(node.getAttributes().item(k).getNodeValue()); attributes.append("\""); } formatted.append(indent); formatted.append("<"); formatted.append(node.getNodeName()); formatted.append(attributes.toString()); if (!node.hasChildNodes()) { formatted.append("/>\n"); return formatted.toString(); } if ((node.hasChildNodes() && node.getFirstChild().getNodeType() == Node.TEXT_NODE)) { formatted.append(">"); } else { formatted.append(">\n"); } for (int i = 0; i < node.getChildNodes().getLength(); i++) { formatted.append(format(node.getChildNodes().item(i), indent + " ")); } if (node.hasChildNodes() && node.getFirstChild().getNodeType() != Node.TEXT_NODE) { formatted.append(indent); } formatted.append("</"); formatted.append(node.getNodeName()); formatted.append(">\n"); } else { String value = node.getTextContent().trim(); if (value.length() > 0) { formatted.append(value); } } return formatted.toString(); }
From source file:Main.java
public static String getElementValue(Node node, String nodeName) { NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if ((child.getNodeType() == Node.ELEMENT_NODE) && (child.getNodeName().equals(nodeName))) return getElementValue(child); }/*ww w . jav a2 s . c o m*/ return null; }
From source file:Main.java
/** * Returns the child element with the specified tagName for the specified parent element * // w w w .j a v a 2s .c o m * @param parent * @param tagName * @return */ public static Element getChildElementByTagName(Element parent, String tagName) { if (parent == null || tagName == null) { return null; } NodeList nodes = parent.getChildNodes(); Node node; int len = nodes.getLength(); for (int i = 0; i < len; i++) { node = nodes.item(i); if (node.getNodeType() == Node.ELEMENT_NODE && ((Element) node).getNodeName().equals(tagName)) { return (Element) node; } } return null; }
From source file:Main.java
public static String getChildElementValueByTagName(Element parentElement, String childTag) { if (childTag.equals(parentElement.getNodeName())) { return getNodeValue(parentElement); }/*from w w w . j a v a 2s . c o m*/ for (Node temp = parentElement.getFirstChild(); temp != null; temp = temp.getNextSibling()) { if (temp.getNodeType() == Node.ELEMENT_NODE && childTag.equals(temp.getNodeName())) { return getNodeValue(temp); } } return null; }
From source file:Main.java
public static Element findPreviousElement(final Node current, final boolean sameName) { String name = null;// ww w. j ava 2s . c o m if (sameName) { name = current.getNodeName(); } int type = Node.ELEMENT_NODE; return (Element) getPrevious(current, name, type); }
From source file:Main.java
/** * @param name The name of the child elements you want * @return a List of child Elements/*from w w w. ja va 2s . c o m*/ */ public static List<Element> getChildren(Element element, String name) throws Exception { NodeList nodes = element.getChildNodes(); ArrayList<Element> ret = new ArrayList<Element>(nodes.getLength()); for (int i = 0; i < nodes.getLength(); i++) { Node childNode = nodes.item(i); if (childNode.getNodeName().equals(name) && childNode.getNodeType() == Node.ELEMENT_NODE) { ret.add((Element) childNode); } } return ret; }
From source file:Main.java
/** * equivalent to the XPath expression './/tagName[@attrName='attrValue']' *///from w w w. ja v a 2 s .c o m public static Element getElementByAttributeValue(Node start, String tagName, String attrName, String attrValue) { NodeList nl = ((Element) start).getElementsByTagName(tagName); int l = nl.getLength(); if (l == 0) { return null; } Element e = null; String compareValue = null; for (int i = 0; i < l; i++) { e = (Element) nl.item(i); if (e.getNodeType() == Node.ELEMENT_NODE) { compareValue = e.getAttribute(attrName); if (compareValue.equals(attrValue)) { return e; } } } return null; }
From source file:Main.java
public static Element first(Element root, String namepaceUri, String localName) { if (root == null) return null; for (Node n1 = root.getFirstChild(); n1 != null; n1 = n1.getNextSibling()) { if (n1.getNodeType() != Node.ELEMENT_NODE) continue; Element e2 = Element.class.cast(n1); if (namepaceUri.equals(e2.getNamespaceURI()) && e2.getLocalName().equals(localName)) { return e2; }//from ww w.ja v a 2 s . c om } return null; }
From source file:Main.java
/** * Get element node string value./*from www . j a v a 2 s .c o m*/ * * @param element element to get string value for * @return concatenated text node descendant values */ public static String getStringValue(final Element element) { final StringBuilder buf = new StringBuilder(); final NodeList children = element.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { final Node n = children.item(i); switch (n.getNodeType()) { case Node.TEXT_NODE: buf.append(n.getNodeValue()); break; case Node.ELEMENT_NODE: buf.append(getStringValue((Element) n)); break; } } return buf.toString(); }