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 void assertEquivalent(Node node, Node node2) { if (node == null) { throw new IllegalArgumentException("the first node to be compared is null"); }/*from w ww.j av a2s. c o m*/ if (node2 == null) { throw new IllegalArgumentException("the second node to be compared is null"); } if (!node.getNodeName().equals(node2.getNodeName())) { throw new IllegalArgumentException("nodes have different node names"); } int attrCount = 0; NamedNodeMap attrs = node.getAttributes(); if (attrs != null) { attrCount = attrs.getLength(); } int attrCount2 = 0; NamedNodeMap attrs2 = node2.getAttributes(); if (attrs2 != null) { attrCount2 = attrs2.getLength(); } if (attrCount != attrCount2) { throw new IllegalArgumentException("nodes hava a different number of attributes"); } outer: for (int i = 0; i < attrCount; i++) { Node n = attrs.item(i); String name = n.getNodeName(); String value = n.getNodeValue(); for (int j = 0; j < attrCount; j++) { Node n2 = attrs2.item(j); String name2 = n2.getNodeName(); String value2 = n2.getNodeValue(); if (name.equals(name2) && value.equals(value2)) { continue outer; } } throw new IllegalArgumentException("attribute " + name + "=" + value + " doesn't match"); } boolean hasChildren = node.hasChildNodes(); if (hasChildren != node2.hasChildNodes()) { throw new IllegalArgumentException("one node has children and the other doesn't"); } if (hasChildren) { NodeList nl = node.getChildNodes(); NodeList nl2 = node2.getChildNodes(); short[] toFilter = new short[] { Node.TEXT_NODE, Node.ATTRIBUTE_NODE, Node.COMMENT_NODE }; List nodes = filter(nl, toFilter); List nodes2 = filter(nl2, toFilter); int length = nodes.size(); if (length != nodes2.size()) { throw new IllegalArgumentException("nodes hava a different number of children"); } for (int i = 0; i < length; i++) { Node n = (Node) nodes.get(i); Node n2 = (Node) nodes2.get(i); assertEquivalent(n, n2); } } }
From source file:Main.java
public static String getTextData(Element element) { NodeList childNodes = element.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { if ((childNodes.item(i).getNodeType() == Node.TEXT_NODE) || (childNodes.item(i).getNodeType() == Node.CDATA_SECTION_NODE)) { if (childNodes.item(i) != null) { String tmpStr = ((Text) childNodes.item(i)).getWholeText().trim(); if (tmpStr.length() > 0) { return tmpStr; }// www . j a v a 2s . co m } } } return null; }
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 w w w . j a v a2s . c o m*/ public static String getElementContent(Element element, String defaultStr) throws Exception { if (element == null) { return defaultStr; } final NodeList children = element.getChildNodes(); final StringBuilder result = new StringBuilder(""); 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.append(children.item(i).getNodeValue()); } // else if ( children.item( i ).getNodeType() == Node.COMMENT_NODE ) { // // Ignore comment nodes // } } return result.toString().trim(); }
From source file:de.betterform.xml.xforms.action.MessageAction.java
private String evaluateMessageContent() throws XFormsException { String message = ""; if (this.element.hasChildNodes()) { NodeList childNodes = this.element.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node node = childNodes.item(i); switch (node.getNodeType()) { case Node.TEXT_NODE: message += node.getTextContent(); break; case Node.ELEMENT_NODE: Element element = (Element) node; Object userData = element.getUserData(""); if ((userData != null) && userData instanceof Output) { Output output = (Output) userData; message += (String) output.computeValueAttribute(); }/*w w w . j a va2 s . c om*/ break; } } } return message; }
From source file:Main.java
/** * Copies the source tree into the specified place in a destination * tree. The source node and its children are appended as children * of the destination node.// w w w.ja v a2 s. c o m * <p> * <em>Note:</em> This is an iterative implementation. */ public static void copyInto(Node src, Node dest) throws DOMException { // get node factory Document factory = dest.getOwnerDocument(); boolean domimpl = factory instanceof DocumentImpl; // placement variables Node start = src; Node parent = src; 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); if (domimpl && !attr.getSpecified()) { ((AttrImpl) element.getAttributeNode(attrName)).setSpecified(false); } } 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 + " (" + node.getNodeName() + ')'); } } dest.appendChild(node); // iterate over children if (place.hasChildNodes()) { parent = place; place = place.getFirstChild(); dest = node; } // advance else { place = place.getNextSibling(); while (place == null && parent != start) { place = parent.getNextSibling(); parent = parent.getParentNode(); dest = dest.getParentNode(); } } } }
From source file:Main.java
private static Node getChildNodeByType(Element element, short nodeType) { if (element == null) return null; NodeList nodes = element.getChildNodes(); if (nodes == null || nodes.getLength() < 1) return null; Node node;//from ww w . ja v a 2s.com String data; for (int i = 0; i < nodes.getLength(); i++) { node = nodes.item(i); short type = node.getNodeType(); if (type == nodeType) { if (type == Node.TEXT_NODE || type == Node.CDATA_SECTION_NODE) { data = ((Text) node).getData(); if (data == null || data.trim().length() < 1) continue; } return node; } } return null; }
From source file:Main.java
/** * Serializes the DOM-tree to a stringBuffer. Recursive method! * * @param node Node to start examining the tree from. * @param writeString The StringBuffer you want to fill with xml. * @return The StringBuffer containing the xml. * // w w w. j ava2 s . co m * @since 2002-12-12 * @author Mattias Bogeblad */ public static StringBuffer serializeDom(Node node, StringBuffer writeString) { int type = node.getNodeType(); try { switch (type) { // print the document element case Node.DOCUMENT_NODE: { writeString.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); writeString = serializeDom(((Document) node).getDocumentElement(), writeString); break; } // print element with attributes case Node.ELEMENT_NODE: { writeString.append("<"); writeString.append(node.getNodeName()); NamedNodeMap attrs = node.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i); String outString = " " + attr.getNodeName() + "=\"" + replaceSpecialCharacters(attr.getNodeValue()) + "\""; writeString.append(outString); } writeString.append(">"); NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) writeString = serializeDom(children.item(i), writeString); } break; } // handle entity reference nodes case Node.ENTITY_REFERENCE_NODE: { String outString = "&" + node.getNodeName() + ";"; writeString.append(outString); break; } // print cdata sections case Node.CDATA_SECTION_NODE: { String outString = "<![CDATA[" + node.getNodeValue() + "]]>"; writeString.append(outString); break; } // print text case Node.TEXT_NODE: { writeString.append(replaceSpecialCharacters(node.getNodeValue())); break; } // print processing instruction case Node.PROCESSING_INSTRUCTION_NODE: { String data = node.getNodeValue(); String outString = "<?" + node.getNodeName() + " " + data + "?>"; writeString.append(outString); break; } } if (type == Node.ELEMENT_NODE) { String outString = "</" + node.getNodeName() + ">"; writeString.append(outString); } } catch (Exception e) { } return writeString; }
From source file:Main.java
/** * Obtains the text of the specified element. * * @param element the text element./* w w w . j a v a2s .c o m*/ * @return the text value inside the element. */ public static String getText(final Element element) { final Node node = element.getFirstChild(); if (node != null && node.getNodeType() == Node.TEXT_NODE) { return node.getNodeValue().trim(); } return null; }
From source file:com.autentia.tnt.xml.UtilitiesXML.java
/** * Devuelve el texto de un nodo: <tag>TEXTO</tag> * @param n/*from w w w .ja v a 2s.c om*/ * @return */ public static String getTexto(Node n) { NodeList nl = n.getChildNodes(); Node act = null; for (int i = 0; i < nl.getLength(); i++) { act = nl.item(i); if (act == null) return null; if ((act.getNodeType() == Node.CDATA_SECTION_NODE) || (act.getNodeType() == Node.TEXT_NODE)) return act.getNodeValue(); } return ""; }
From source file:Main.java
/** Prints the specified node, recursively. * * @param node Node to be printed//from w ww . j a v a 2 s. c om */ public static void print(Node node) { // is there anything to do? if (node == null) { return; } System.out.println(""); int type = node.getNodeType(); switch (type) { // print document case Node.DOCUMENT_NODE: { /* if (!canonical) { if (Encoding.equalsIgnoreCase("DEFAULT")) Encoding = "UTF-8"; else if(Encoding.equalsIgnoreCase("Unicode")) Encoding = "UTF-16"; else Encoding = MIME2Java.reverse(Encoding); out.println("<?xml version=\"1.0\" encoding=\"" + Encoding + "\"?>"); } */ print(((Document) node).getDocumentElement()); out.flush(); break; } // print element with attributes case Node.ELEMENT_NODE: { out.print('<'); out.print(node.getNodeName()); Attr attrs[] = sortAttributes(node.getAttributes()); for (int i = 0; i < attrs.length; i++) { Attr attr = attrs[i]; out.print(' '); out.print(attr.getNodeName()); out.print("=\""); out.print(normalize(attr.getNodeValue())); out.print('"'); } out.print('>'); NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) { print(children.item(i)); } } break; } // handle entity reference nodes case Node.ENTITY_REFERENCE_NODE: { if (canonical) { NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) { print(children.item(i)); } } } else { out.print('&'); out.print(node.getNodeName()); out.print(';'); } break; } // print cdata sections case Node.CDATA_SECTION_NODE: { if (canonical) { out.print(normalize(node.getNodeValue())); } else { out.print("<![CDATA["); out.print(node.getNodeValue()); out.print("]]>"); } break; } // print DocumentType sections case Node.DOCUMENT_TYPE_NODE: { out.print("<!DOCTYPE "); out.print(((DocumentType) node).getName()); out.print(" SYSTEM "); out.print(((DocumentType) node).getSystemId()); out.print(">"); break; } // print text case Node.TEXT_NODE: { out.print(normalize(node.getNodeValue())); break; } // print processing instruction case Node.PROCESSING_INSTRUCTION_NODE: { out.print("<?"); out.print(node.getNodeName()); String data = node.getNodeValue(); if (data != null && data.length() > 0) { out.print(' '); out.print(data); } out.print("?>"); break; } } if (type == Node.ELEMENT_NODE) { out.print("</"); out.print(node.getNodeName()); out.print('>'); } out.flush(); }