List of usage examples for org.w3c.dom Node CDATA_SECTION_NODE
short CDATA_SECTION_NODE
To view the source code for org.w3c.dom Node CDATA_SECTION_NODE.
Click Source Link
CDATASection
. From source file:Main.java
public static Object getContent(Element element) { NodeList nl = element.getChildNodes(); StringBuilder content = new StringBuilder(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i);//from ww w. ja v a2 s . c o m switch (node.getNodeType()) { case Node.ELEMENT_NODE: return node; case Node.CDATA_SECTION_NODE: case Node.TEXT_NODE: content.append(node.getNodeValue()); break; } } return content.toString().trim(); }
From source file:Main.java
public String getNodeType(Node node) { String type;//from w ww .j a v a 2 s . c om switch (node.getNodeType()) { case Node.ELEMENT_NODE: { type = "Element"; break; } case Node.ATTRIBUTE_NODE: { type = "Attribute"; break; } case Node.TEXT_NODE: { type = "Text"; break; } case Node.CDATA_SECTION_NODE: { type = "CData section"; break; } case Node.ENTITY_REFERENCE_NODE: { type = "Entity reference"; break; } case Node.ENTITY_NODE: { type = "Entity"; break; } case Node.PROCESSING_INSTRUCTION_NODE: { type = "Processing instruction"; break; } case Node.COMMENT_NODE: { type = "Comment"; break; } case Node.DOCUMENT_NODE: { type = "Document"; break; } case Node.DOCUMENT_TYPE_NODE: { type = "Document type"; break; } case Node.DOCUMENT_FRAGMENT_NODE: { type = "Document fragment"; break; } case Node.NOTATION_NODE: { type = "Notation"; break; } default: { type = "???"; break; } } return type; }
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./*from ww w. j a va2s . co 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
/** Get string value of an element. * @param element Element/* w w w .j av a 2s .c o m*/ * @return String of the node. Empty string if nothing found. */ public static String getString(final Element element) { final Node text = element.getFirstChild(); if (text == null) // <empty /> node return ""; if ((text.getNodeType() == Node.TEXT_NODE || text.getNodeType() == Node.CDATA_SECTION_NODE)) return text.getNodeValue(); return ""; }
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./*ww w.j ava 2 s . 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
/** * Returns the text content of the provided element as is. If multiple text * DOM nodes are present, they are concatenated together. * //from w w w . j av a2 s . co m * @param element The element that contains the desired text content. * @return The text content of the given element. * @throws Exception If an exception occurs during the traversal of the DOM * objects. */ public static String getElementTextData(Element element) throws Exception { if (!element.hasChildNodes()) { throw new Exception("Element has no children."); } Node n = element.getFirstChild(); if ((n.getNodeType() != Node.TEXT_NODE) && (n.getNodeType() != Node.CDATA_SECTION_NODE)) { // must be a textual node // for now throw an exception, but later need to just skip this module and // resume initialization throw new Exception("Element child node is not textual."); } CharacterData value = (CharacterData) n; return value.getData(); }
From source file:Main.java
/** * Check if the element contains just a text/cdata, in which case return * that value. Else return null;/* ww w .ja v a2 s . c o m*/ * * @param element * @return null if this is not a textElement as we see it. Value of single * text/CData child otherwise */ private static String getElementValue(Element element) { if (element.getAttributes().getLength() > 0) { return null; } NodeList children = element.getChildNodes(); String value = null; int nbrChildren = children.getLength(); for (int i = 0; i < nbrChildren; i++) { Node child = children.item(i); short childType = child.getNodeType(); if (childType == Node.ELEMENT_NODE) { return null; } if (childType == Node.CDATA_SECTION_NODE || childType == Node.TEXT_NODE) { if (value != null) { return null; } value = child.getNodeValue(); } } return value; }
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 ava 2 s .c o m public static String getElementContent(Element element, String defaultStr) throws Exception { if (element == null) { return defaultStr; } NodeList children = element.getChildNodes(); 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:Main.java
public static String text(Node node) { StringBuffer sb = new StringBuffer(); Node n = node.getFirstChild(); while (n != null) { if (n.getNodeType() == Node.TEXT_NODE || n.getNodeType() == Node.CDATA_SECTION_NODE) sb.append(n.getNodeValue()); n = n.getNextSibling();/*ww w . j a va2 s. co m*/ } return sb.toString(); }
From source file:MainClass.java
public void processNode(Node node, String spacer) throws IOException { if (node == null) return;/* ww w . j a v a 2s. c o m*/ switch (node.getNodeType()) { case Node.ELEMENT_NODE: String name = node.getNodeName(); System.out.print(spacer + "<" + name); NamedNodeMap nnm = node.getAttributes(); for (int i = 0; i < nnm.getLength(); i++) { Node current = nnm.item(i); System.out.print(" " + current.getNodeName() + "= " + current.getNodeValue()); } System.out.print(">"); NodeList nl = node.getChildNodes(); if (nl != null) { for (int i = 0; i < nl.getLength(); i++) { processNode(nl.item(i), ""); } } System.out.println(spacer + "</" + name + ">"); break; case Node.TEXT_NODE: System.out.print(node.getNodeValue()); break; case Node.CDATA_SECTION_NODE: System.out.print("" + node.getNodeValue() + ""); break; case Node.ENTITY_REFERENCE_NODE: System.out.print("&" + node.getNodeName() + ";"); break; case Node.ENTITY_NODE: System.out.print("<ENTITY: " + node.getNodeName() + "> </" + node.getNodeName() + "/>"); break; case Node.DOCUMENT_NODE: NodeList nodes = node.getChildNodes(); if (nodes != null) { for (int i = 0; i < nodes.getLength(); i++) { processNode(nodes.item(i), ""); } } break; case Node.DOCUMENT_TYPE_NODE: DocumentType docType = (DocumentType) node; System.out.print("<!DOCTYPE " + docType.getName()); if (docType.getPublicId() != null) { System.out.print(" PUBLIC " + docType.getPublicId() + " "); } else { System.out.print(" SYSTEM "); } System.out.println(" " + docType.getSystemId() + ">"); break; default: break; } }