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
/** * Return the content of the given element. * <p/>//from w w w . j a va 2s. c o m * We will descend to an arbitrary depth looking for the first text node. * <p/> * Note that the parser may break what was originally a single string of * pcdata into multiple adjacent text nodes. Xerces appears to do this when * it encounters a '$' in the text, not sure if there is specified behavior, * or if its parser specific. * <p/> * Here, we will congeal adjacent text nodes. * <p/> * We will NOT ignore text nodes that have only whitespace. */ public static String getContent(Element e) { String content = null; if (e != null) { // find the first inner text node, Text t = findText(e, false); if (t != null) { // we have at least some text StringBuilder b = new StringBuilder(); while (t != null) { b.append(t.getData()); Node n = t.getNextSibling(); t = null; if (n != null && ((n.getNodeType() == Node.TEXT_NODE) || (n.getNodeType() == Node.CDATA_SECTION_NODE))) { t = (Text) n; } } content = b.toString(); } } return content; }
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 ww.j ava 2s. co m*/ public final 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
/** * 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. *//*from w ww . j a v a 2 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
private static boolean checkNodeTypes(Node childNode) { short nodeType = childNode.getNodeType(); if (nodeType == Node.ELEMENT_NODE) { cleanEmptyTextNodes(childNode); // recurse into subtree }/*from www. j av a2 s .co m*/ if (nodeType == Node.ELEMENT_NODE || nodeType == Node.CDATA_SECTION_NODE || nodeType == Node.COMMENT_NODE) { return true; } else { return false; } }
From source file:Main.java
/** Prints the specified node, then prints all of its children. */ public static void printDOM(Node node) { int type = node.getNodeType(); switch (type) { // print the document element case Node.DOCUMENT_NODE: { System.out.print("<?xml version=\"1.0\" ?>"); printDOM(((Document) node).getDocumentElement()); break;/*from w w w .j a va 2 s. co m*/ } // print element with attributes case Node.ELEMENT_NODE: { System.out.println(); System.out.print("<"); System.out.print(node.getNodeName()); NamedNodeMap attrs = node.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i); System.out.print(" " + attr.getNodeName().trim() + "=\"" + attr.getNodeValue().trim() + "\""); } System.out.print(">"); NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) printDOM(children.item(i)); } break; } // handle entity reference nodes case Node.ENTITY_REFERENCE_NODE: { System.out.print("&"); System.out.print(node.getNodeName().trim()); System.out.print(";"); break; } // print cdata sections case Node.CDATA_SECTION_NODE: { System.out.print("<![CDATA["); System.out.print(node.getNodeValue().trim()); System.out.print("]]>"); break; } // print text case Node.TEXT_NODE: { System.out.println(); System.out.print(node.getNodeValue().trim()); break; } // print processing instruction case Node.PROCESSING_INSTRUCTION_NODE: { System.out.print("<?"); System.out.print(node.getNodeName().trim()); String data = node.getNodeValue().trim(); { System.out.print(" "); System.out.print(data); } System.out.print("?>"); break; } } if (type == Node.ELEMENT_NODE) { System.out.println(); System.out.print("</"); System.out.print(node.getNodeName().trim()); System.out.print('>'); } }
From source file:Main.java
/** * Returns element's CDATA Node/*from w w w. j a v a 2 s. c om*/ * * @param element * the element which CDATA node is returned * @return CDATA node */ public static CDATASection getElementCDataNode(Element element) { return (CDATASection) getChildNodeByType(element, Node.CDATA_SECTION_NODE); }
From source file:Main.java
/** * For compatibility reasons the following is required: * If the value of a text node is to be changed, but a CDATA section with this name * already exists, the CDATA section is removed an a text node is created or changed. * * If the value of a CDATA section is to be changed, but a text node with this name * already exists, the text node is removed an a CDATA section is created or changed. * *//* w w w .j a va 2 s . c om*/ public static void setElementText(Element e, String newValue, boolean cdata) { if (e == null) { return; } Node node = null; NodeList children = e.getChildNodes(); if (children != null) { Node childToRemove = null; boolean changed = false; int listLength = children.getLength(); for (int i = 0; i < listLength; i++) { node = children.item(i); int nodeType = node.getNodeType(); if (nodeType == Node.TEXT_NODE) { if (cdata) { childToRemove = node; } else { node.setNodeValue(newValue); changed = true; } } if (nodeType == Node.CDATA_SECTION_NODE) { if (!cdata) { childToRemove = node; } else { node.setNodeValue(newValue); changed = true; } } } if (childToRemove != null) { // System.out.println("removing child " + childToRemove.getNodeValue()); childToRemove.setNodeValue(""); e.removeChild(childToRemove); } if (changed) { return; } } Document doc = e.getOwnerDocument(); if (cdata) { node = doc.createCDATASection(newValue); } else { node = doc.createTextNode(newValue); } e.appendChild(node); }
From source file:TreeDumper2.java
private void dumpLoop(Node node, String indent) { switch (node.getNodeType()) { case Node.ATTRIBUTE_NODE: dumpAttributeNode((Attr) node, indent); break;//from w ww. j av a 2s.c o m case Node.CDATA_SECTION_NODE: dumpCDATASectionNode((CDATASection) node, indent); break; case Node.COMMENT_NODE: dumpCommentNode((Comment) node, indent); break; case Node.DOCUMENT_NODE: dumpDocument((Document) node, indent); break; case Node.DOCUMENT_FRAGMENT_NODE: dumpDocumentFragment((DocumentFragment) node, indent); break; case Node.DOCUMENT_TYPE_NODE: dumpDocumentType((DocumentType) node, indent); break; case Node.ELEMENT_NODE: dumpElement((Element) node, indent); break; case Node.ENTITY_NODE: dumpEntityNode((Entity) node, indent); break; case Node.ENTITY_REFERENCE_NODE: dumpEntityReferenceNode((EntityReference) node, indent); break; case Node.NOTATION_NODE: dumpNotationNode((Notation) node, indent); break; case Node.PROCESSING_INSTRUCTION_NODE: dumpProcessingInstructionNode((ProcessingInstruction) node, indent); break; case Node.TEXT_NODE: dumpTextNode((Text) node, indent); break; default: System.out.println(indent + "Unknown node"); break; } NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) dumpLoop(list.item(i), indent + " "); }
From source file:Main.java
/** * @param node/* ww w . ja va 2 s . c o 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 w ww . jav a2s .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(); }