List of usage examples for org.w3c.dom Node getNodeType
public short getNodeType();
From source file:Main.java
public static String getElementText(Element e) { if (e == null) { return (""); }//from w w w .j av a 2 s.com NodeList children = e.getChildNodes(); if (children == null) { return (""); } StringBuffer text = new StringBuffer(); int listLength = children.getLength(); for (int i = 0; i < listLength; i++) { Node node = children.item(i); int nodeType = node.getNodeType(); if ((nodeType == Node.TEXT_NODE) || (nodeType == Node.CDATA_SECTION_NODE)) { String nodeValue = node.getNodeValue(); if (nodeValue != null) { text.append(nodeValue); } } } return (text.toString().trim()); }
From source file:Main.java
public static String toString(NodeList nodes) { try {/* www . j ava2 s . c o m*/ StringWriter stw = new StringWriter(); Transformer serializer = TransformerFactory.newInstance().newTransformer(); for (int i = 0; i < nodes.getLength(); i++) { Node item = nodes.item(i); switch (item.getNodeType()) { case Node.TEXT_NODE: stw.append(item.getTextContent()); break; default: serializer.transform(new DOMSource(item), new StreamResult(stw)); } } return stw.toString(); } catch (Exception e) { return e.toString(); } }
From source file:Main.java
private static boolean isEmptyTextNode(Node node) { if (node.getNodeType() == Node.TEXT_NODE) { String str = node.getNodeValue(); if (str == null || str.trim().length() == 0) { return true; }//from ww w . j a v a 2 s . c o m } return false; }
From source file:Utils.java
/** * <p>Returns an array of text values of a child element. Returns * <code>null</code> if there is no child element found.</p> * * @param parent parent element/*from ww w . j a va 2 s .com*/ * @param name name of the child element * @return text value */ public static String[] getChildElementTextArr(Element parent, String name) { // Get all the elements List children = getChildElementsByName(parent, name); String str[] = new String[children.size()]; for (int i = 0; i < children.size(); i++) { Node child = (Node) children.get(i); StringBuffer buf = new StringBuffer(); NodeList nodes = child.getChildNodes(); for (int j = 0; j < nodes.getLength(); j++) { Node node = nodes.item(j); if (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE) { Text text = (Text) node; buf.append(text.getData().trim()); } } str[i] = buf.toString(); } return str; }
From source file:Main.java
public static Element findSingleElement(Element parent, String fullXPath) { Element elt = null;/*w w w . j a va 2 s . c om*/ if (parent != null) { if (parent.hasChildNodes()) { if (fullXPath.startsWith("/")) fullXPath = fullXPath.substring(1); int index = fullXPath.indexOf("/"); String childName = ((index != -1) ? fullXPath.substring(0, index) : fullXPath); NodeList list = parent.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node child = list.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { if (child.getNodeName().equalsIgnoreCase(childName)) { if (index == -1) { elt = (Element) child; break; } else { fullXPath = fullXPath.substring(index + 1); elt = findSingleElement((Element) child, fullXPath); } } } } } } return elt; }
From source file:Main.java
public static Element getFirstChild(Element paramElement, String paramString1, String paramString2, String paramString3) {/*from www .j a v a 2 s. com*/ NodeList localNodeList = paramElement.getChildNodes(); for (int i = 0; i < localNodeList.getLength(); i++) { Node localNode = localNodeList.item(i); if ((localNode != null) && (localNode.getNodeType() == 1) && (((Element) localNode).getTagName().equals(paramString1))) { Element localElement = (Element) localNode; if ((localElement.hasAttribute(paramString2)) && (paramString3.equals(localElement.getAttribute(paramString2)))) return localElement; } } return null; }
From source file:Main.java
public static Object toObject(Node node) { if (node.getNodeType() == Node.DOCUMENT_NODE) { node = node.getFirstChild();/*from w ww . java 2s .c o m*/ Map<String, Object> map = new LinkedHashMap<>(1); map.put(node.getNodeName(), toObject(node)); return map; } Object value = getElementValue(node); if (node.hasAttributes()) { Map<String, Object> wrapper = new LinkedHashMap<>(2); wrapper.put("_", value); wrapper.put("@", getAttributes(node)); return wrapper; } else { return value; } }
From source file:Main.java
/** * Method getFullTextChildrenFromElement * * @param element//from w w w .j a v a2 s.co m * @return the string of children */ public static String getFullTextChildrenFromElement(Element element) { StringBuilder sb = new StringBuilder(); Node child = element.getFirstChild(); while (child != null) { if (child.getNodeType() == Node.TEXT_NODE) { sb.append(((Text) child).getData()); } child = child.getNextSibling(); } return sb.toString(); }
From source file:Main.java
/** Takes XML node and prints to String. * * @param node Element to print to String * @return XML node as String/*from w w w . ja va 2s .co m*/ */ private static void printNode(StringBuffer sBuffer, Node node) { if (node.getNodeType() == Node.TEXT_NODE) { sBuffer.append(encodeXMLText(node.getNodeValue().trim())); } else if (node.getNodeType() == Node.CDATA_SECTION_NODE) { sBuffer.append("<![CDATA["); sBuffer.append(node.getNodeValue()); sBuffer.append("]]>"); } else if (node.getNodeType() == Node.ELEMENT_NODE) { Element el = (Element) node; sBuffer.append("<").append(el.getTagName()); NamedNodeMap attribs = el.getAttributes(); for (int i = 0; i < attribs.getLength(); i++) { Attr nextAtt = (Attr) attribs.item(i); sBuffer.append(" ").append(nextAtt.getName()).append("=\"").append(nextAtt.getValue()).append("\""); } NodeList nodes = node.getChildNodes(); if (nodes.getLength() == 0) { sBuffer.append("/>"); } else { sBuffer.append(">"); for (int i = 0; i < nodes.getLength(); i++) { printNode(sBuffer, nodes.item(i)); } sBuffer.append("</").append(el.getTagName()).append(">"); } } }
From source file:Main.java
/** * Returns the MIME type of the XSLT transformation output taken from the "media-type" attribute of the xsl:output element. * If the "media-type" attribute is not used it takes the "method" attribute of the same element. * The following mappings between the "method" attribute values and MIME types are used * text -> text/plain/*from ww w . ja v a 2s. c om*/ * xml -> application/xml * html -> text/html * @param xslName * @return */ public static String getOutputMimeType(String xslName) { String mimeType = ""; String mediaType = ""; String method = ""; getDocument(xslName); NodeList nList = xsl.getElementsByTagName("xsl:output"); for (int i = 0; i < nList.getLength(); i++) { Node outputNode = nList.item(i); if (outputNode.getNodeType() == Node.ELEMENT_NODE) { Element outputElement = (Element) outputNode; mediaType = outputElement.getAttribute("media-type"); method = outputElement.getAttribute("method"); } } if ("".equals(mediaType)) { mimeType = ("text".equals(method)) ? "text/plain" : ("xml".equals(method)) ? "application/xml" : ("html".equals(method)) ? "text/html" : ""; } else mimeType = mediaType; return mimeType; }