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
/** * Obtains the list of child element nodes. * * @param element the root element.// w ww . j a v a 2s. c o m * @return the list of child elements. */ private static List<Element> getElementList(final Element element) { final List<Element> elementList = new ArrayList<Element>(); final NodeList childNodes = element.getChildNodes(); final int numChildren = childNodes.getLength(); for (int i = 0; i < numChildren; i++) { final Node childNode = childNodes.item(i); if (childNode.getNodeType() != Node.ELEMENT_NODE) { continue; } elementList.add((Element) childNode); } return elementList; }
From source file:Main.java
/** * Get the first child element of the specified (root) element that has the specified name. If no element by the * specified name is found, null is returned. Note that more than one element with the same name may be a child of * root. This method simply returns the first one. * /*ww w. j a v a2s.c om*/ * @param root The element to search. * @param name The local name of the element to look for. * @return The element if found, null otherwise. */ public static Element getElement(Element root, String name) { if (root == null || name == null || name.length() <= 0) throw new IllegalArgumentException("Null or invalid argument passed to XmlUtil.getElement()"); NodeList lst = root.getChildNodes(); int size = lst.getLength(); name = localName(name); for (int i = 0; i < size; i++) { Node node = lst.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { String nodeName = localName(node); if (name.equals(nodeName)) return (Element) node; } } return null; }
From source file:Main.java
/** * Retrieves the child element with the specified tag name for the given element. * * @param elem The element whose child element is to be retrieved. * @param tagName Name of the child element. * @pre elem != null && tagName != null && !tagName.equals("") * @return The text contents of the specified sub element, or null if the subelement * does not exist./*from w w w.ja v a 2s . c o m*/ */ public static Element getChildElement(Element elem, String tagName) { NodeList children = elem.getChildNodes(); Element childElement = null; int index = 0; while (childElement == null && index < children.getLength()) { Node child = children.item(index); if (child.getNodeType() == Node.ELEMENT_NODE && ((Element) child).getTagName().equals(tagName)) { childElement = (Element) child; } else { index++; } } return childElement; }
From source file:Main.java
/** * returns the Node Type As String// w w w .j a v a2s .c o m * @param node * @param cftype * @return */ public static String getTypeAsString(Node node, boolean cftype) { String suffix = cftype ? "" : "_NODE"; switch (node.getNodeType()) { case Node.ATTRIBUTE_NODE: return "ATTRIBUTE" + suffix; case Node.CDATA_SECTION_NODE: return "CDATA_SECTION" + suffix; case Node.COMMENT_NODE: return "COMMENT" + suffix; case Node.DOCUMENT_FRAGMENT_NODE: return "DOCUMENT_FRAGMENT" + suffix; case Node.DOCUMENT_NODE: return "DOCUMENT" + suffix; case Node.DOCUMENT_TYPE_NODE: return "DOCUMENT_TYPE" + suffix; case Node.ELEMENT_NODE: return "ELEMENT" + suffix; case Node.ENTITY_NODE: return "ENTITY" + suffix; case Node.ENTITY_REFERENCE_NODE: return "ENTITY_REFERENCE" + suffix; case Node.NOTATION_NODE: return "NOTATION" + suffix; case Node.PROCESSING_INSTRUCTION_NODE: return "PROCESSING_INSTRUCTION" + suffix; case Node.TEXT_NODE: return "TEXT" + suffix; default: return "UNKNOW" + suffix; } }
From source file:Main.java
/** * Returns the next sibling Element for an element, optionally matching tag names. * /*w ww. j av a 2s . c om*/ * @param child the element from which to search for a next sibling * @param sameName true to retrive the next sibling element of the same name, of false if any name is allowed * @return the next sibling element if one exists, or null otherwise */ static public Element getNextSiblingElement(Element child, boolean sameName) { // Child Element suchen if (child == null) return null; String name = child.getTagName(); Node node = child.getNextSibling(); while (node != null) { // Find all Element nodes if (node.getNodeType() == Node.ELEMENT_NODE) { // check name Element elem = (Element) node; if (sameName && name.equalsIgnoreCase(elem.getTagName())) return elem; // found } node = node.getNextSibling(); } return null; // not found! }
From source file:Main.java
private static void prettyPrintLoop(Node node, StringBuilder string, String indentation) { if (node == null) { return;//from w w w .j a v a2s . c o m } int type = node.getNodeType(); switch (type) { case Node.DOCUMENT_NODE: string.append("\n"); prettyPrintLoop(node.getChildNodes(), string, indentation + "\t"); break; case Node.ELEMENT_NODE: string.append(indentation); string.append("<"); string.append(node.getNodeName()); Attr[] attributes; if (node.getAttributes() != null) { int length = node.getAttributes().getLength(); attributes = new Attr[length]; for (int loopIndex = 0; loopIndex < length; loopIndex++) { attributes[loopIndex] = (Attr) node.getAttributes().item(loopIndex); } } else { attributes = new Attr[0]; } for (Attr attribute : attributes) { string.append(" "); string.append(attribute.getNodeName()); string.append("=\""); string.append(attribute.getNodeValue()); string.append("\""); } string.append(">\n"); prettyPrintLoop(node.getChildNodes(), string, indentation + "\t"); string.append(indentation); string.append("</"); string.append(node.getNodeName()); string.append(">\n"); break; case Node.TEXT_NODE: string.append(indentation); string.append(node.getNodeValue().trim()); string.append("\n"); break; case Node.PROCESSING_INSTRUCTION_NODE: string.append(indentation); string.append("<?"); string.append(node.getNodeName()); String text = node.getNodeValue(); if (text != null && text.length() > 0) { string.append(text); } string.append("?>\n"); break; case Node.CDATA_SECTION_NODE: string.append(indentation); string.append("<![CDATA["); string.append(node.getNodeValue()); string.append("]]>"); break; } }
From source file:Main.java
/** * Checks if a node has a child of ELEMENT type. * /*from w w w.ja v a 2 s .c o m*/ * @param node * a node * @return true if the node has a child of ELEMENT type */ public static boolean hasElementChild(Node node) { NodeList nl = node.getChildNodes(); Node child = null; int length = nl.getLength(); for (int i = 0; i < length; i++) { child = nl.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { return true; } } return false; }
From source file:Main.java
public static void copyInto(Node src, Node dest) throws DOMException { Document factory = dest.getOwnerDocument(); //Node start = src; Node parent = null;/*from w ww . j a va2 s . c o m*/ Node place = src; // traverse source tree while (place != null) { // copy this node Node node = null; int type = place.getNodeType(); switch (type) { case Node.CDATA_SECTION_NODE: { node = factory.createCDATASection(place.getNodeValue()); break; } case Node.COMMENT_NODE: { node = factory.createComment(place.getNodeValue()); break; } case Node.ELEMENT_NODE: { Element element = factory.createElement(place.getNodeName()); node = element; NamedNodeMap attrs = place.getAttributes(); int attrCount = attrs.getLength(); for (int i = 0; i < attrCount; i++) { Attr attr = (Attr) attrs.item(i); String attrName = attr.getNodeName(); String attrValue = attr.getNodeValue(); element.setAttribute(attrName, attrValue); } break; } case Node.ENTITY_REFERENCE_NODE: { node = factory.createEntityReference(place.getNodeName()); break; } case Node.PROCESSING_INSTRUCTION_NODE: { node = factory.createProcessingInstruction(place.getNodeName(), place.getNodeValue()); break; } case Node.TEXT_NODE: { node = factory.createTextNode(place.getNodeValue()); break; } default: { throw new IllegalArgumentException( "can't copy node type, " + type + " (" + place.getNodeName() + ')'); } } dest.appendChild(node); // iterate over children if (place.hasChildNodes()) { parent = place; place = place.getFirstChild(); dest = node; } else if (parent == null) { place = null; } else { // advance place = place.getNextSibling(); while (place == null && parent != null) { place = parent.getNextSibling(); parent = parent.getParentNode(); dest = dest.getParentNode(); } } } }
From source file:Main.java
/** * Search all child nodes of the given for the first element that has the * specified tag name./*from w w w. ja v a 2 s . c o m*/ * * @param aStartNode * The parent element to be searched. May not be <code>null</code>. * @param sName * The tag name to search. * @return <code>null</code> if the parent element has no such child element. */ @Nullable public static Element getFirstChildElementOfName(@Nonnull final Node aStartNode, @Nullable final String sName) { final NodeList aNodeList = aStartNode.getChildNodes(); final int nLen = aNodeList.getLength(); for (int i = 0; i < nLen; ++i) { final Node aNode = aNodeList.item(i); if (aNode.getNodeType() == Node.ELEMENT_NODE) { final Element aElement = (Element) aNode; if (aElement.getTagName().equals(sName)) return aElement; } } return null; }
From source file:Main.java
/** * Determines the index (starting at 0) of the given element relative to * other element nodes for the same parent. *///w w w . ja va 2s .c o m public static int getElementPosition(Element element) { int num = -1; Node node = element; while (node != null) { if (node.getNodeType() == Node.ELEMENT_NODE) { ++num; } node = node.getPreviousSibling(); } return num; }