List of usage examples for org.w3c.dom Node getNodeType
public short getNodeType();
From source file:Main.java
/** Return a list of all the children of a given node *///w w w . j a v a 2 s.co m static public Vector<Node> findAllChildren(Node node) { if (node == null) return null; Vector<Node> found_children = new Vector<Node>(); NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) found_children.add(child); } return found_children; }
From source file:Main.java
/** * @return a list of all child nodes with this name *//* w ww . j a v a2 s . co m*/ public static ArrayList<Node> getChildNodes(Node node, String name) { ArrayList<Node> results = new ArrayList<Node>(); NodeList nodeList = node.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node temp = nodeList.item(i); if (temp.getNodeType() != Node.ELEMENT_NODE) continue; if (name.equals(temp.getLocalName()) || name.equals(temp.getNodeName())) { results.add(temp); } } return (results); }
From source file:Main.java
/** * Finds element in DOM tree//from w w w. ja v a2 s . c om * @param topElm Top element * @param nodeName Node name * @return returns found node */ public static List<Node> findNodesByType(Element topElm, int type) { List<Node> retvals = new ArrayList<Node>(); if (topElm == null) throw new IllegalArgumentException("topElm cannot be null"); synchronized (topElm.getOwnerDocument()) { Stack<Node> stack = new Stack<Node>(); stack.push(topElm); while (!stack.isEmpty()) { Node curElm = stack.pop(); if (curElm.getNodeType() == type) { retvals.add(curElm); } List<Node> nodesToProcess = new ArrayList<Node>(); NodeList childNodes = curElm.getChildNodes(); for (int i = 0, ll = childNodes.getLength(); i < ll; i++) { Node item = childNodes.item(i); //stack.push((Element) item); nodesToProcess.add(item); } Collections.reverse(nodesToProcess); for (Node node : nodesToProcess) { stack.push(node); } } return retvals; } }
From source file:Main.java
/** * Determine whether an element has any child elements. * @param element the element to check.// www . j ava2 s . co m * @return true if the element has child elements; false otherwise. */ public static boolean hasChildElements(Element element) { Node child = element.getFirstChild(); while (child != null) { if (child.getNodeType() == Node.ELEMENT_NODE) return true; child = child.getNextSibling(); } return false; }
From source file:Main.java
public static void setXsdSchema(Node node, String schemaURL) { Document doc;/*from w ww. ja va 2 s . co m*/ if (node.getNodeType() != Node.DOCUMENT_NODE) { doc = node.getOwnerDocument(); } else { doc = (Document) node; } Element root = doc.getDocumentElement(); if (schemaURL == null) { root.removeAttribute("xmlns:xsi"); root.removeAttribute("xsi:noNamespaceSchemaLocation"); } else { root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); root.setAttribute("xsi:noNamespaceSchemaLocation", schemaURL); } }
From source file:Main.java
/** * Transform this {@link Node} into a {@link String} representation. * * @param xml/*from w ww .j a v a 2 s. c om*/ * the xml Node * @return a String representation of the given xml Node */ public static String toString(Node xml) { short type = xml.getNodeType(); if (type == Node.TEXT_NODE) return xml.getNodeValue(); StringWriter buffer = new StringWriter(); try { TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(xml), new StreamResult(buffer)); } catch (IllegalArgumentException | TransformerException e) { //throw new XMLHandlingException("Cannot create String from Node '" + xml + "'."); return ""; } return buffer.toString(); }
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.//from w w w . j av a 2 s . com * @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
/** * return the text content of an element *//*from w ww . j a va2 s .com*/ public static String getTextContent(org.w3c.dom.Node element) { StringBuffer childtext = new StringBuffer(); NodeList childlist = element.getChildNodes(); int ct = childlist.getLength(); for (int j = 0; j < ct; j++) { org.w3c.dom.Node childNode = childlist.item(j); if ((childNode.getNodeType() == Node.TEXT_NODE) || (childNode.getNodeType() == Node.CDATA_SECTION_NODE)) { childtext.append(childNode.getNodeValue().trim()); } } return childtext.toString(); }
From source file:Main.java
/** * Method to get the depth of a XML Element. * @param element the XML Element.//from ww w . ja v a 2 s . com * @return the int depth of the XML Element. */ public static int getDepth(Element element) { Node parent = element.getParentNode(); int depth = 0; while (parent != null && parent.getNodeType() == Node.ELEMENT_NODE) { depth++; parent = parent.getParentNode(); } return depth; }
From source file:Main.java
/** * Get Element that has attribute id="xyz". *//*from ww w . j a v a 2 s .c om*/ public static Element getElementById(NodeList nodeList, String id) { // Note we should really use the Query here !! Element element = null; int len = nodeList.getLength(); for (int i = 0; i < len; i++) { Node node = (Node) nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { element = (Element) node; if (((Element) node).getAttribute("id").equals(id)) { // found it ! break; } } } // returns found element or null return element; }