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
public static String getFullTextFromChildren(Element element) { if (element == null) { return null; }// w ww.ja va 2s .c o m StringBuffer sb = new StringBuffer(1000); NodeList nl = element.getChildNodes(); Node child = null; int length = nl.getLength(); for (int i = 0; i < length; i++) { child = nl.item(i); if (child.getNodeType() == Node.TEXT_NODE) { sb.append(child.getNodeValue()); } } return sb.toString().trim(); }
From source file:Main.java
@SuppressWarnings("fallthrough") static final void getSetRec(final Node rootNode, final Set<Node> result, final Node exclude, final boolean com) { //Set result = new HashSet(); if (rootNode == exclude) { return;//from ww w .j a v a2 s . co m } switch (rootNode.getNodeType()) { case Node.ELEMENT_NODE: result.add(rootNode); Element el = (Element) rootNode; if (el.hasAttributes()) { NamedNodeMap nl = ((Element) rootNode).getAttributes(); for (int i = 0; i < nl.getLength(); i++) { result.add(nl.item(i)); } } //no return keep working - ignore fallthrough warning case Node.DOCUMENT_NODE: for (Node r = rootNode.getFirstChild(); r != null; r = r.getNextSibling()) { if (r.getNodeType() == Node.TEXT_NODE) { result.add(r); while ((r != null) && (r.getNodeType() == Node.TEXT_NODE)) { r = r.getNextSibling(); } if (r == null) return; } getSetRec(r, result, exclude, com); } return; case Node.COMMENT_NODE: if (com) { result.add(rootNode); } return; case Node.DOCUMENT_TYPE_NODE: return; default: result.add(rootNode); } return; }
From source file:Main.java
private static void traverseNode(Node parentNode, JsonObject parentJson, JsonObject upperJson) { NodeList childList = parentNode.getChildNodes(); for (int i = 0; i < childList.getLength(); i++) { JsonObject childJson = new JsonObject(); Node childNode = childList.item(i); if (childNode.getNodeType() == Node.TEXT_NODE) { if (childNode.getNodeValue().trim().length() != 0) { // non empty text node reached, so add to the parent processTextNode(parentNode, upperJson, childJson, childNode); }/*ww w . j a v a2 s. c o m*/ } else if (childNode.getNodeType() == Node.ELEMENT_NODE) { if (childNode.hasAttributes()) { // attributes exist, so go thru them traverseAttributes(childJson, childNode); } if (childNode.hasChildNodes()) { // child nodes exist, so go into them traverseNode(childNode, childJson, parentJson); } if (childNode.getNodeType() != Node.TEXT_NODE) { // non text node element if (ADDED_BY_VALUE.contains(childNode)) { // already added as a value if (parentJson.has(childNode.getNodeName())) { // there is already such an element as expected JsonElement existing = parentJson.get(childNode.getNodeName()); if (existing instanceof JsonPrimitive) { // it is a primitive as expected Iterator attrs = childJson.entrySet().iterator(); if (attrs.hasNext()) { // there are attributes, so reorganize the element to include the attributes and the // value as property - #text reorganizeForAttributes(parentJson, childNode, existing, attrs); } } else if (existing instanceof JsonArray) { // already added and reorganized as an array, so take the last element of this type and // add the attributes Iterator attrs = childJson.entrySet().iterator(); if (attrs.hasNext()) { reorganizeAddAttributes(childNode, attrs); } } else if (existing instanceof JsonObject) { System.err.println("ERROR: found object, but expected primitive or array"); } } else { System.err.println("ERROR: expected element, but it does not exist"); } // remove it from the list ADDED_BY_VALUE.remove(childNode); } else { if (parentJson.has(childNode.getNodeName())) { // parent already has such an element JsonElement existing = parentJson.get(childNode.getNodeName()); if (existing instanceof JsonArray) { // and it is already an array, so just add the child to the array ((JsonArray) existing).add(childJson); } else if (existing instanceof JsonObject) { // and it not an array, so reorganize the element reorganizeElement(parentNode, parentJson, childJson, childNode, existing); } } else { // no such an element yet, so add it to the parent parentJson.add(childNode.getNodeName(), childJson); } } } } else if (childNode.getNodeType() == Node.CDATA_SECTION_NODE) { // processTextNode(parentNode, upperJson, childJson, childNode); String base64 = Base64.getEncoder().encodeToString(childNode.getNodeValue().getBytes()); parentJson.addProperty(childNode.getNodeName(), base64); } else { System.err.println("ERROR: unsupported node type: " + childNode.getNodeType()); } } }
From source file:Main.java
public static void copyInto(Node src, Node dest) throws DOMException { Document factory = dest.getOwnerDocument(); //Node start = src; Node parent = null;//from www. j a v a 2 s . c o m Node place = src; // traverse source tree while (place != null) { // copy this node Node node = null; int type = place.getNodeType(); switch (type) { case Node.CDATA_SECTION_NODE: { node = factory.createCDATASection(place.getNodeValue()); break; } case Node.COMMENT_NODE: { node = factory.createComment(place.getNodeValue()); break; } case Node.ELEMENT_NODE: { Element element = factory.createElement(place.getNodeName()); node = element; NamedNodeMap attrs = place.getAttributes(); int attrCount = attrs.getLength(); for (int i = 0; i < attrCount; i++) { Attr attr = (Attr) attrs.item(i); String attrName = attr.getNodeName(); String attrValue = attr.getNodeValue(); element.setAttribute(attrName, attrValue); } break; } case Node.ENTITY_REFERENCE_NODE: { node = factory.createEntityReference(place.getNodeName()); break; } case Node.PROCESSING_INSTRUCTION_NODE: { node = factory.createProcessingInstruction(place.getNodeName(), place.getNodeValue()); break; } case Node.TEXT_NODE: { node = factory.createTextNode(place.getNodeValue()); break; } default: { throw new IllegalArgumentException( "can't copy node type, " + type + " (" + place.getNodeName() + ')'); } } dest.appendChild(node); // iterate over children if (place.hasChildNodes()) { parent = place; place = place.getFirstChild(); dest = node; } else if (parent == null) { place = null; } else { // advance place = place.getNextSibling(); while (place == null && parent != null) { place = parent.getNextSibling(); parent = parent.getParentNode(); dest = dest.getParentNode(); } } } }
From source file:Main.java
/** * @param node// www . j av a 2 s .co m * @throws IOException */ public static void serializeNode(Node node) throws IOException { if (writer == null) writer = new BufferedWriter(new OutputStreamWriter(System.out)); switch (node.getNodeType()) { case Node.DOCUMENT_NODE: Document doc = (Document) node; writer.write("<?xml version=\""); writer.write(doc.getXmlVersion()); writer.write("\" encoding=\"UTF-8\" standalone=\""); if (doc.getXmlStandalone()) writer.write("yes"); else writer.write("no"); writer.write("\"?>\n"); NodeList nodes = node.getChildNodes(); if (nodes != null) for (int i = 0; i < nodes.getLength(); i++) serializeNode(nodes.item(i)); break; case Node.ELEMENT_NODE: String name = node.getNodeName(); writer.write("<" + name); NamedNodeMap attributes = node.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Node current = attributes.item(i); writer.write(" " + current.getNodeName() + "=\""); print(current.getNodeValue()); writer.write("\""); } writer.write(">"); NodeList children = node.getChildNodes(); if (children != null) { //if ((children.item(0) != null) && (children.item(0).getNodeType() == Node.ELEMENT_NODE)) // writer.write("\n"); for (int i = 0; i < children.getLength(); i++) serializeNode(children.item(i)); if ((children.item(0) != null) && (children.item(children.getLength() - 1).getNodeType() == Node.ELEMENT_NODE)) writer.write(""); } writer.write("</" + name + ">"); break; case Node.TEXT_NODE: print(node.getNodeValue()); break; case Node.CDATA_SECTION_NODE: writer.write("CDATA"); print(node.getNodeValue()); writer.write(""); break; case Node.COMMENT_NODE: writer.write("<!-- " + node.getNodeValue() + " -->\n"); break; case Node.PROCESSING_INSTRUCTION_NODE: writer.write("<?" + node.getNodeName() + " " + node.getNodeValue() + "?>\n"); break; case Node.ENTITY_REFERENCE_NODE: writer.write("&" + node.getNodeName() + ";"); break; case Node.DOCUMENT_TYPE_NODE: DocumentType docType = (DocumentType) node; String publicId = docType.getPublicId(); String systemId = docType.getSystemId(); String internalSubset = docType.getInternalSubset(); writer.write("<!DOCTYPE " + docType.getName()); if (publicId != null) writer.write(" PUBLIC \"" + publicId + "\" "); else writer.write(" SYSTEM "); writer.write("\"" + systemId + "\""); if (internalSubset != null) writer.write(" [" + internalSubset + "]"); writer.write(">\n"); break; } writer.flush(); }
From source file:Main.java
/** * Get the content of the given element. * * @param element The element to get the content for. * @param defaultStr The default to return when there is no content. * @return The content of the element or the default. *//*from ww w.j a v a 2 s.c o m*/ public static String getElementContent(Element element, String defaultStr) throws Exception { if (element == null) return defaultStr; NodeList children = element.getChildNodes(); String result = ""; for (int i = 0; i < children.getLength(); i++) { if (children.item(i).getNodeType() == Node.TEXT_NODE || children.item(i).getNodeType() == Node.CDATA_SECTION_NODE) { result += children.item(i).getNodeValue(); } else if (children.item(i).getNodeType() == Node.COMMENT_NODE) { // Ignore comment nodes } } return result.trim(); }
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; }/* w w w .j av a 2s.c om*/ } return false; }
From source file:de.betterform.xml.xforms.action.SetValueAction.java
/** * Performs element init.// ww w . j av a 2 s. c o m * * @throws XFormsException if any error occurred during init. */ public void init() throws XFormsException { super.init(); this.valueAttribute = getXFormsAttribute(VALUE_ATTRIBUTE); if (this.valueAttribute == null) { Node child = this.element.getFirstChild(); if ((child != null) && (child.getNodeType() == Node.TEXT_NODE)) { this.nodeValue = child.getNodeValue(); } else { this.nodeValue = ""; } } }
From source file:Main.java
private static void prettyPrintLoop(Node node, StringBuilder string, String indentation) { if (node == null) { return;//from w w w . j a v a 2 s . com } int type = node.getNodeType(); switch (type) { case Node.DOCUMENT_NODE: string.append("\n"); prettyPrintLoop(node.getChildNodes(), string, indentation + "\t"); break; case Node.ELEMENT_NODE: string.append(indentation); string.append("<"); string.append(node.getNodeName()); Attr[] attributes; if (node.getAttributes() != null) { int length = node.getAttributes().getLength(); attributes = new Attr[length]; for (int loopIndex = 0; loopIndex < length; loopIndex++) { attributes[loopIndex] = (Attr) node.getAttributes().item(loopIndex); } } else { attributes = new Attr[0]; } for (Attr attribute : attributes) { string.append(" "); string.append(attribute.getNodeName()); string.append("=\""); string.append(attribute.getNodeValue()); string.append("\""); } string.append(">\n"); prettyPrintLoop(node.getChildNodes(), string, indentation + "\t"); string.append(indentation); string.append("</"); string.append(node.getNodeName()); string.append(">\n"); break; case Node.TEXT_NODE: string.append(indentation); string.append(node.getNodeValue().trim()); string.append("\n"); break; case Node.PROCESSING_INSTRUCTION_NODE: string.append(indentation); string.append("<?"); string.append(node.getNodeName()); String text = node.getNodeValue(); if (text != null && text.length() > 0) { string.append(text); } string.append("?>\n"); break; case Node.CDATA_SECTION_NODE: string.append(indentation); string.append("<![CDATA["); string.append(node.getNodeValue()); string.append("]]>"); break; } }
From source file:Main.java
private static String getXPathFromVector(Vector path) { StringBuffer strBuf = new StringBuffer(); int length = path.size(); for (int i = 0; i < length; i++) { Node tempNode = (Node) path.elementAt(i); short nodeType = getNodeType(tempNode); String targetValue = getValue(tempNode, nodeType); int position = 1; tempNode = getPreviousTypedNode(tempNode, nodeType); while (tempNode != null) { if (nodeType == Node.ELEMENT_NODE) { if (getValue(tempNode, nodeType).equals(targetValue)) { position++;// ww w . j ava 2 s .c o m } } else { position++; } tempNode = getPreviousTypedNode(tempNode, nodeType); } boolean hasMatchingSiblings = (position > 1); if (!hasMatchingSiblings) { tempNode = (Node) path.elementAt(i); tempNode = getNextTypedNode(tempNode, nodeType); while (!hasMatchingSiblings && tempNode != null) { if (nodeType == Node.ELEMENT_NODE) { if (getValue(tempNode, nodeType).equals(targetValue)) { hasMatchingSiblings = true; } else { tempNode = getNextTypedNode(tempNode, nodeType); } } else { hasMatchingSiblings = true; } } } String step; switch (nodeType) { case Node.TEXT_NODE: step = "text()"; break; case Node.PROCESSING_INSTRUCTION_NODE: step = "processing-instruction()"; break; default: step = targetValue; break; } if (step != null && step.length() > 0) { strBuf.append('/' + step); } if (hasMatchingSiblings) { strBuf.append("[" + position + "]"); } } return strBuf.toString(); }