List of usage examples for org.w3c.dom Node getNodeType
public short getNodeType();
From source file:Main.java
/** * Get XML <code>Node</code> text content. This method duplicates the * org.w3c.dom.Node.getTextContent() method in JDK 1.5. * * @param baseNode The XML node from which the content is being retrieved. * @return The text content of the XML node. *///ww w . ja v a 2s . co m public static String getTextContent(Node baseNode) { // if element, first child will be a text element with content Node child = baseNode.getFirstChild(); if (child != null && child.getNodeType() == Node.TEXT_NODE) { return child.getNodeValue(); } return ""; }
From source file:Main.java
static public ArrayList<Element> selectElements(Element element, String xpathExpression) throws Exception { ArrayList<Element> resultVector = new ArrayList<Element>(); if (element == null) { return resultVector; }/* ww w. ja v a2s . com*/ 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)) { resultVector.add((Element) node); } } } else { XPath xpath = XPathFactory.newInstance().newXPath(); NodeList nodes = (NodeList) xpath.evaluate(xpathExpression, element, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); resultVector.add((Element) node); } } return resultVector; }
From source file:Main.java
public static String getXMLValueByName(String xmlFilePath, String name) { String returnValue = "No Value!"; DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); try {/*from ww w .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
public static void stripWhitespace(Element element) { final NodeList childNodeList = element.getChildNodes(); for (int i = 0; i < childNodeList.getLength(); i++) { Node node = childNodeList.item(i); switch (node.getNodeType()) { case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: final String text = ((CharacterData) node).getData(); if (WhitespacePattern.matcher(text).matches()) { element.removeChild(node); --i;// w w w. j av a2 s. c o m } break; case Node.ELEMENT_NODE: stripWhitespace((Element) node); break; } } }
From source file:Main.java
public static Node getFirstTextNode(Node node) { NodeList nl = ((Element) node).getChildNodes(); Node ret = null;/* w w w. j a v a 2 s . co m*/ int i = 0; while (ret == null && i < nl.getLength()) { Node n = nl.item(i++); if (n.getNodeType() == Node.TEXT_NODE) ret = n; } return ret; }
From source file:Main.java
/** * This method extracts the list of elements from the @param element that contains the @param tagName. * * @param tagName the tag name used to get the list of elements * @param element the dom element to look for the tags with the given name * @return list of elements that have the @param tagName *///from ww w .j av a 2 s .c o m public static List<Element> getElementsByTagName(String tagName, Element element) { List<Element> elements = new ArrayList<>(); NodeList nodeList = element.getElementsByTagName(tagName); if (nodeList != null && nodeList.getLength() > 0) { for (int index = 0; index < nodeList.getLength(); index++) { Node currentNode = nodeList.item(index); if (currentNode.getNodeType() == Node.ELEMENT_NODE) { elements.add((Element) currentNode); } } } return elements; }
From source file:Main.java
protected static Map<String, String> createAttributeTable(Element e) { Map<String, String> attributeTable = new HashMap<>(); NamedNodeMap attrs = e.getAttributes(); if (attrs != null) { int numAttrs = attrs.getLength(); for (int i = 0; i < numAttrs; i++) { Node na = attrs.item(i); if (na.getNodeType() != Node.ATTRIBUTE_NODE) { continue; }//from w w w. ja v a2s . c o m Attr a = (Attr) na; attributeTable.put(a.getName(), a.getValue()); } } return attributeTable; }
From source file:Main.java
/** * Gets the text value of the given element. * /* www. j a va 2 s . com*/ * @param e the element. * @return the text contents of the given element.s */ public static String getText(Element e) { StringBuilder sb = null; if (e != null) { NodeList children = e.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node node = children.item(i); switch (node.getNodeType()) { case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: if (sb == null) { sb = new StringBuilder(); } sb.append(node.getNodeValue()); break; } } } return (sb != null) ? sb.toString() : null; }
From source file:Main.java
public static String getCDATAContent(Node node) { NodeList nodeList = node.getChildNodes(); if (nodeList != null) { for (int i = 0; i < nodeList.getLength(); i++) { Node child = nodeList.item(i); if (child.getNodeType() == Node.CDATA_SECTION_NODE) { CDATASection cdata = (CDATASection) child; return cdata.getData(); }/*from w w w . jav a 2 s . com*/ } } return null; }
From source file:Main.java
public static void removeAll(Node node, short nodeType, String name) { if (node.getNodeType() == nodeType && (name == null || node.getNodeName().equals(name))) { node.getParentNode().removeChild(node); } else {/*from ww w . j ava 2s. com*/ // Visit the children NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { removeAll(list.item(i), nodeType, name); } } }