List of usage examples for org.w3c.dom Node TEXT_NODE
short TEXT_NODE
To view the source code for org.w3c.dom Node TEXT_NODE.
Click Source Link
Text
node. From source file:Main.java
private static String prettyPrintDom(Node node, String indent, boolean isRoot, boolean escapeStrings) { String ret = ""; switch (node.getNodeType()) { case Node.DOCUMENT_NODE: // recurse on each child NodeList nodes = node.getChildNodes(); if (nodes != null) { for (int i = 0; i < nodes.getLength(); i++) { ret += prettyPrintDom(nodes.item(i), indent, isRoot, escapeStrings); }// www .j a v a2 s.com } break; case Node.ELEMENT_NODE: String name = node.getNodeName(); ret += indent + "<" + name; NamedNodeMap attributes = node.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Node current = attributes.item(i); ret += " " + current.getNodeName() + "=\"" + ((escapeStrings) ? escapeStringForXML(current.getNodeValue()) : current.getNodeValue()) + "\""; } ret += ">"; // recurse on each child NodeList children = node.getChildNodes(); if (children != null) { for (int i = 0; i < children.getLength(); i++) { String tmp = prettyPrintDom(children.item(i), indent + ((isRoot) ? "" : BI), false, escapeStrings); if (!tmp.replaceAll("[\\s]+", "").equals("")) if (tmp.endsWith("\n")) if (ret.endsWith("\n")) ret += tmp; else ret += "\n" + tmp; else ret += tmp; } } if (ret.endsWith("\n")) ret += indent + "</" + name + ">\n"; else ret += "</" + name + ">\n"; break; case Node.TEXT_NODE: ret += (escapeStrings) ? escapeStringForXML(node.getNodeValue()) : node.getNodeValue(); break; case Node.COMMENT_NODE: ret += "<!-- " + node.getNodeValue() + " -->"; break; } return ret; }
From source file:Main.java
/** * returns contents of first TextNode defined within an XML Node * //from w w w. j a va 2 s . co m * @param xmlNode parsed XML Node in which to look for a TextNode * @return text character string value of text node **/ public static String xmlFindTextNode(final Node xmlNode) { if (xmlNode == null) { return null; } final NodeList children = xmlNode.getChildNodes(); final int childrenCnt = size(children); for (int j = 0; j < childrenCnt; j++) { final Node childNode = children.item(j); if ((childNode != null) && (childNode.getNodeType() == Node.TEXT_NODE)) { return childNode.getNodeValue(); } } return null; }
From source file:Main.java
/** * Gets Node type String for a given node type constant *//*from w w w . ja va 2 s . c om*/ public static String getNodeTypeStr(int nodeType) { switch (nodeType) { case Node.ATTRIBUTE_NODE: return "ATTRIBUTE_NODE "; case Node.CDATA_SECTION_NODE: return "CDATA_SECTION_NODE"; case Node.COMMENT_NODE: return "COMMENT_NODE"; case Node.DOCUMENT_FRAGMENT_NODE: return "DOCUMENT_FRAGMENT_NODE"; case Node.DOCUMENT_TYPE_NODE: return "DOCUMENT_TYPE_NODE"; case Node.ELEMENT_NODE: return "ELEMENT_NODE"; case Node.ENTITY_NODE: return "ENTITY_NODE"; case Node.ENTITY_REFERENCE_NODE: return "ENTITY_REFERENCE_NODE"; case Node.NOTATION_NODE: return "NOTATION_NODE"; case Node.PROCESSING_INSTRUCTION_NODE: return "PROCESSING_INSTRUCTION_NODE"; case Node.TEXT_NODE: return "TEXT_NODE"; case Node.DOCUMENT_NODE: return "DOCUMENT_NODE"; default: return "UN-INDENTIFIED NODE"; } }
From source file:Main.java
/** * * @param xmlContent//from w w w. j a va 2s . c o m * @param charset * @param expression * @return * @throws SAXException * @throws IOException * @throws ParserConfigurationException * @throws XPathExpressionException */ public static List<String> getValues(String xmlContent, Charset charset, String expression) throws SAXException, IOException, ParserConfigurationException, XPathExpressionException { List<String> valueList = new ArrayList<String>(); NodeList nodeList = getNodeList(xmlContent, expression); int length = nodeList.getLength(); for (int seq = 0; seq < length; seq++) { Node node = nodeList.item(seq); if (node.getNodeType() == Node.ELEMENT_NODE) { NodeList childNodeList = node.getChildNodes(); StringBuilder sBuilder = new StringBuilder(); for (int i = 0; i < childNodeList.getLength(); i++) { Node childNode = childNodeList.item(i); if (childNode.getNodeType() == Node.TEXT_NODE) { sBuilder.append(((Text) childNode).getNodeValue()); } } valueList.add(sBuilder.toString()); } else if (node.getNodeType() == Node.ATTRIBUTE_NODE) { valueList.add(node.getNodeValue()); } } return valueList; }
From source file:Main.java
public static String toString(NodeList nodes) { try {// w w w.j a v a 2s . 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
/** * Clone given Node into target Document. If targe is null, same Document will be used. * If deep is specified, all children below will also be cloned. *///w w w . java2 s. co m public static Node cloneNode(Node node, Document target, boolean deep) throws DOMException { if (target == null || node.getOwnerDocument() == target) // same Document return node.cloneNode(deep); else { //DOM level 2 provides this in Document, so once xalan switches to that, //we can take out all the below and just call target.importNode(node, deep); //For now, we implement based on the javadocs for importNode Node newNode; int nodeType = node.getNodeType(); switch (nodeType) { case Node.ATTRIBUTE_NODE: newNode = target.createAttribute(node.getNodeName()); break; case Node.DOCUMENT_FRAGMENT_NODE: newNode = target.createDocumentFragment(); break; case Node.ELEMENT_NODE: Element newElement = target.createElement(node.getNodeName()); NamedNodeMap nodeAttr = node.getAttributes(); if (nodeAttr != null) for (int i = 0; i < nodeAttr.getLength(); i++) { Attr attr = (Attr) nodeAttr.item(i); if (attr.getSpecified()) { Attr newAttr = (Attr) cloneNode(attr, target, true); newElement.setAttributeNode(newAttr); } } newNode = newElement; break; case Node.ENTITY_REFERENCE_NODE: newNode = target.createEntityReference(node.getNodeName()); break; case Node.PROCESSING_INSTRUCTION_NODE: newNode = target.createProcessingInstruction(node.getNodeName(), node.getNodeValue()); break; case Node.TEXT_NODE: newNode = target.createTextNode(node.getNodeValue()); break; case Node.CDATA_SECTION_NODE: newNode = target.createCDATASection(node.getNodeValue()); break; case Node.COMMENT_NODE: newNode = target.createComment(node.getNodeValue()); break; case Node.NOTATION_NODE: case Node.ENTITY_NODE: case Node.DOCUMENT_TYPE_NODE: case Node.DOCUMENT_NODE: default: throw new IllegalArgumentException("Importing of " + node + " not supported yet"); } if (deep) for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) newNode.appendChild(cloneNode(child, target, true)); return newNode; } }
From source file:Main.java
static String getTextValue(Node node) throws SAXException { Node textNode = node.getFirstChild(); if (textNode == null) return ""; if (textNode.getNodeType() != Node.TEXT_NODE) throw new SAXException("No text value found for <" + node.getNodeName() + "> node"); return textNode.getNodeValue(); }
From source file:Main.java
/** * Searches the node for content of type Long. If non-long content is found, * it logs a warning and returns null./* w w w . ja va2s .com*/ */ public static String extractStringFromElement(Node element) { if (element == null) { return null; } else if (element.getFirstChild() == null) { return null; } else if (element.getFirstChild().getNodeValue() == null) { return null; } else { // Get all the children NodeList children = element.getChildNodes(); StringBuffer output = new StringBuffer(); for (int n = 0; n < children.getLength(); n++) { Node child = children.item(n); if (child.getNodeType() == Node.ENTITY_REFERENCE_NODE) { output.append(child.getFirstChild().getNodeValue()); } else if (child.getNodeType() == Node.CDATA_SECTION_NODE) { output.append(child.getFirstChild().getNodeValue()); } else if (child.getNodeType() == Node.ENTITY_NODE) { output.append(child.getFirstChild().getNodeValue()); } else if (child.getNodeType() == Node.TEXT_NODE) { output.append(child.getNodeValue()); } } return output.toString().trim(); } }
From source file:Main.java
/** * This method will return the content of this particular <code>element</code>. * For example,/* w w w . j av a 2 s . c o m*/ * * <pre> * <result>something_1</result> * </pre> * When the {@link org.w3c.dom.Element} <code><result></code> is passed in as * argument (<code>element</code> to this method, it returns the content of it, * namely, <code>something_1</code> in the example above. * * @return */ public static String getContent(Element element) { StringBuffer paramValue = new StringBuffer(); NodeList childNodes = element.getChildNodes(); for (int j = 0; j < childNodes.getLength(); j++) { Node currentNode = childNodes.item(j); if (currentNode != null && currentNode.getNodeType() == Node.TEXT_NODE) { String val = currentNode.getNodeValue(); if (val != null) { paramValue.append(val.trim()); } } } String val = paramValue.toString().trim(); return val; }
From source file:Main.java
/** * * @param xmlContent//from w w w . ja v a 2 s. c o m * @param charset * @param expression * @return * @throws SAXException * @throws IOException * @throws ParserConfigurationException * @throws XPathExpressionException */ public static List<String> getHexBinaryValues(String xmlContent, Charset charset, String expression) throws SAXException, IOException, ParserConfigurationException, XPathExpressionException { List<String> valueList = new ArrayList<String>(); NodeList nodeList = getNodeList(xmlContent, expression); int length = nodeList.getLength(); for (int seq = 0; seq < length; seq++) { Node node = nodeList.item(seq); if (node.getNodeType() == Node.ELEMENT_NODE) { NodeList childNodeList = node.getChildNodes(); StringBuilder sBuilder = new StringBuilder(); for (int i = 0; i < childNodeList.getLength(); i++) { Node childNode = childNodeList.item(i); if (childNode.getNodeType() == Node.TEXT_NODE) { sBuilder.append(((Text) childNode).getNodeValue()); } } valueList.add(printHexBinary(sBuilder.toString(), charset)); } else if (node.getNodeType() == Node.ATTRIBUTE_NODE) { valueList.add(node.getNodeValue()); } } return valueList; }