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
/** * Get only the XML element children of an XML element. * * @param parent/*from w ww . j av a 2 s.co m*/ * The parent element to get children from * @return The list of children Element of the parent */ public static List<Element> getChildElements(Element parent) { NodeList childNodes = parent.getChildNodes(); List<Element> childElements = new ArrayList<>(); for (int index = 0; index < childNodes.getLength(); index++) { if (childNodes.item(index).getNodeType() == Node.ELEMENT_NODE) { childElements.add((Element) childNodes.item(index)); } } return childElements; }
From source file:Main.java
/** * Finds and returns the last child node with the given name and * attribute name, value pair./*from w ww . j av a 2s . co m*/ */ public static Element getLastChildElement(Node parent, String elemName, String attrName, String attrValue) { // search for node Node child = parent.getLastChild(); while (child != null) { if (child.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) child; if (element.getNodeName().equals(elemName) && element.getAttribute(attrName).equals(attrValue)) { return element; } } child = child.getPreviousSibling(); } // not found return null; }
From source file:Main.java
/** * Finds and returns the first child node with the given name and * attribute name, value pair.//from ww w . j ava 2s. c om */ public static Element getFirstChildElement(Node parent, String elemName, String attrName, String attrValue) { // search for node Node child = parent.getFirstChild(); while (child != null) { if (child.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) child; if (element.getNodeName().equals(elemName) && element.getAttribute(attrName).equals(attrValue)) { return element; } } child = child.getNextSibling(); } // not found return null; }
From source file:Main.java
public static String textContent(Node node) { switch (node.getNodeType()) { case Node.ELEMENT_NODE: StringBuffer sb = new StringBuffer(); Node nextChild = node.getFirstChild(); while (nextChild != null) { sb.append(textContent(nextChild)); nextChild = nextChild.getNextSibling(); }/* w w w . j av a2s. c o m*/ return sb.toString(); case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: return node.getNodeValue(); default: return ""; } }
From source file:Main.java
public static List<Map<String, String>> readXMLFile(String outFile) { DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance(); List<Map<String, String>> returnlist = new ArrayList<Map<String, String>>(); try {/* w w w .j a va 2s .co m*/ DocumentBuilder dombuilder = domfac.newDocumentBuilder(); InputStream is = new FileInputStream(outFile); Document doc = dombuilder.parse(is); NodeList nl = doc.getElementsByTagName("row"); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); NodeList fileds = node.getChildNodes(); Map<String, String> map = new HashMap<String, String>(); for (int j = 0; j < fileds.getLength(); j++) { Node filed = fileds.item(j); if (filed.getNodeType() == Node.ELEMENT_NODE) { map.put(filed.getAttributes().getNamedItem("name").getNodeValue(), filed.getFirstChild().getNodeValue()); } } returnlist.add(map); } } catch (Exception e) { e.printStackTrace(); } return returnlist; }
From source file:Main.java
public static Node findWithAttrubute(Node root, String name, String attributeName) { 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)) { if (subnode.getAttributes().getNamedItem(attributeName) != null) { return subnode; }//from w w w. j a va2 s . c o m } } } return null; }
From source file:Main.java
public static int getCurrentPosition(final Node refNode) { if (refNode == null) { return -1; }//from w ww . j a va 2s . com int counter = 0; Node current = refNode; while (current != null) { if (current.getNodeType() == Node.ELEMENT_NODE) { counter++; } current = current.getPreviousSibling(); } return counter; }
From source file:Main.java
static public Element selectSingleElement(Element element, String xpathExpression) throws XPathExpressionException { if (xpathExpression.indexOf("/") == -1) { NodeList nodeList = element.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(xpathExpression)) { return (Element) node; }//from w w w. j av a 2s . co m } // NodeList nodes = element.getElementsByTagName(xpathExpression); // if (nodes.getLength() > 0) { // return (Element) nodes.item(0); // } else { return null; // } } else { XPath xpath = XPathFactory.newInstance().newXPath(); Element node = (Element) xpath.evaluate(xpathExpression, element, XPathConstants.NODE); return node; } }
From source file:Main.java
public static boolean isTextOnly(Element element) { for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) { if (child.getNodeType() == Node.ELEMENT_NODE) return false; }/* w w w .j av a2 s . co m*/ return true; }
From source file:Main.java
/** * _more_/*from ww w.j av a2 s . c om*/ * * @param html _more_ * @param node _more_ */ public static void toHtml(StringBuffer html, Node node) { switch (node.getNodeType()) { case Node.ELEMENT_NODE: { NodeList children = node.getChildNodes(); int numChildren = children.getLength(); html.append("<b>" + node.getNodeName().replace("_", " ") + "</b>"); html.append(": "); for (int i = 0; i < numChildren; i++) { Node child = children.item(i); if (((child.getNodeType() == Node.TEXT_NODE) || (child.getNodeType() == Node.CDATA_SECTION_NODE))) { String v = child.getNodeValue(); if (v == null) { continue; } if (v.trim().length() == 0) { continue; } html.append(v); html.append(" "); } } boolean didone = false; NamedNodeMap nnm = node.getAttributes(); if (nnm != null) { for (int i = 0; i < nnm.getLength(); i++) { Attr attr = (Attr) nnm.item(i); String attrName = attr.getNodeName(); if (attrName.startsWith("xmlns") || attrName.startsWith("xsi:")) { continue; } if (!didone) { html.append("<ul>"); didone = true; } html.append(attrName.replace("_", " ") + "=" + attr.getNodeValue()); html.append("<br>\n"); } } int cnt = 0; for (int i = 0; i < numChildren; i++) { Node child = children.item(i); if (((child.getNodeType() == Node.TEXT_NODE) || (child.getNodeType() == Node.CDATA_SECTION_NODE))) { continue; } if (!didone) { html.append("<ul>"); didone = true; } if (cnt > 0) { html.append("<br>"); } toHtml(html, child); cnt++; } if (didone) { html.append("</ul>"); } break; } } }