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:edu.duke.cabig.c3pr.webservice.integration.XMLUtils.java
/** * Does deep comparison of two DOM trees represented by the given * {@link Element}s. Elements are considered root nodes of the trees. <br> * <br>// w ww . ja v a 2 s. c om * This version of the method will <b>only</b> process elements of types * {@link Node#ELEMENT_NODE},{@link Node#TEXT_NODE}, * {@link Node#CDATA_SECTION_NODE}; others will be ignored. Content of text * nodes is normalized before comparison. <br> * <br> * When comparing two elements, their local names, namespaces, and * attributes (except <code>xmlns* and xsi:type</code>) are accounted for. <br> * <br> * Due to issues with using * <code>setIgnoringElementContentWhitespace(true)</code>, which does not * seem to work, this method will throw out empty text nodes when comparing * children. * * @since 1.0 * @param e1 * @param e2 * @return */ public static boolean isDeepEqual(Node n1, Node n2) { if (!StringUtils.equals(n1.getLocalName(), n2.getLocalName())) { log.info("Node local names are not equal: " + n1.getLocalName() + " " + n2.getLocalName()); return false; } if (!StringUtils.equals(n1.getNamespaceURI(), n2.getNamespaceURI())) { log.info("Node namespaces are not equal: " + n1.getNamespaceURI() + " " + n2.getNamespaceURI()); return false; } if (n1.getNodeType() != n2.getNodeType()) { log.info("Node types are not equal: " + n1.getNodeType() + " " + n2.getNodeType()); return false; } // check attributes equality. NamedNodeMap attrs1 = n1.getAttributes(); NamedNodeMap attrs2 = n2.getAttributes(); if (!isEqual(attrs1, attrs2)) { return false; } short nodeType = n1.getNodeType(); switch (nodeType) { case Node.ELEMENT_NODE: return isDeepEqual(filterOutEmptyTextNodes(n1.getChildNodes()), filterOutEmptyTextNodes(n2.getChildNodes())); case Node.TEXT_NODE: return isTextNodeEqual(n1, n2); case Node.CDATA_SECTION_NODE: return isTextNodeEqual(n1, n2); default: break; } return true; }
From source file:Main.java
private static void prettyPrintLoop(Node node, StringBuilder string, String indentation) { if (node == null) { return;/*www . j a v a 2 s . c o m*/ } 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
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; }//from ww w. ja v a 2 s . c om } } } 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 ww . j a v a 2 s.c o 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
/** * 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. *//* w w w. ja v a2 s.c om*/ 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: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 w w w. j av a 2 s.c o m*/ 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:DOMDump.java
private void dumpLoop(Node node, String indent) { switch (node.getNodeType()) { case Node.CDATA_SECTION_NODE: System.out.println(indent + "CDATA_SECTION_NODE"); break;//from w w w. ja v a 2 s . c om case Node.COMMENT_NODE: System.out.println(indent + "COMMENT_NODE"); break; case Node.DOCUMENT_FRAGMENT_NODE: System.out.println(indent + "DOCUMENT_FRAGMENT_NODE"); break; case Node.DOCUMENT_NODE: System.out.println(indent + "DOCUMENT_NODE"); break; case Node.DOCUMENT_TYPE_NODE: System.out.println(indent + "DOCUMENT_TYPE_NODE"); break; case Node.ELEMENT_NODE: System.out.println(indent + "ELEMENT_NODE"); break; case Node.ENTITY_NODE: System.out.println(indent + "ENTITY_NODE"); break; case Node.ENTITY_REFERENCE_NODE: System.out.println(indent + "ENTITY_REFERENCE_NODE"); break; case Node.NOTATION_NODE: System.out.println(indent + "NOTATION_NODE"); break; case Node.PROCESSING_INSTRUCTION_NODE: System.out.println(indent + "PROCESSING_INSTRUCTION_NODE"); break; case Node.TEXT_NODE: System.out.println(indent + "TEXT_NODE"); 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:com.autentia.tnt.xml.UtilitiesXML.java
/** * Devuelve el texto de un nodo: <tag>TEXTO</tag> * @param n//from w w w. java2 s .c o m * @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:DOMEdit.java
private static void outputloop(Node node, String indent) { switch (node.getNodeType()) { case Node.ELEMENT_NODE: outputElement((Element) node, indent); break;// w w w . j a va 2s. c om case Node.TEXT_NODE: outputText((Text) node, indent); break; case Node.CDATA_SECTION_NODE: outputCDATASection((CDATASection) node, indent); break; case Node.COMMENT_NODE: outputComment((Comment) node, indent); break; case Node.PROCESSING_INSTRUCTION_NODE: outputProcessingInstructionNode((ProcessingInstruction) node, indent); break; default: System.out.println("Unknown node type: " + node.getNodeType()); break; } }
From source file:Main.java
protected static void print(PrintStream out, Node node) { if (node == null) return;//from w w w. j av a 2 s . c o m short type = node.getNodeType(); switch (type) { case Node.DOCUMENT_NODE: { out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); // out.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"); NodeList nodelist = node.getChildNodes(); int size = nodelist.getLength(); for (int i = 0; i < size; i++) print(out, nodelist.item(i)); break; } case Node.DOCUMENT_TYPE_NODE: { DocumentType docType = (DocumentType) node; out.print("<!DOCTYPE " + getDocumentTypeData(docType) + ">\n"); break; } case Node.ELEMENT_NODE: { out.print('<'); out.print(node.getNodeName()); NamedNodeMap map = node.getAttributes(); if (map != null) { int size = map.getLength(); for (int i = 0; i < size; i++) { Attr attr = (Attr) map.item(i); out.print(' '); out.print(attr.getNodeName()); out.print("=\""); out.print(normalize(attr.getNodeValue())); out.print('"'); } } if (!node.hasChildNodes()) out.print("/>"); else { out.print('>'); NodeList nodelist = node.getChildNodes(); int numChildren = nodelist.getLength(); for (int i = 0; i < numChildren; i++) print(out, nodelist.item(i)); out.print("</"); out.print(node.getNodeName()); out.print('>'); } break; } case Node.ENTITY_REFERENCE_NODE: { NodeList nodelist = node.getChildNodes(); if (nodelist != null) { int size = nodelist.getLength(); for (int i = 0; i < size; i++) print(out, nodelist.item(i)); } break; } case Node.CDATA_SECTION_NODE: { out.print(normalize(node.getNodeValue())); break; } case Node.TEXT_NODE: { out.print(normalize(node.getNodeValue())); break; } case Node.PROCESSING_INSTRUCTION_NODE: { out.print("<?"); out.print(node.getNodeName()); String s = node.getNodeValue(); if (s != null && s.length() > 0) { out.print(' '); out.print(s); } out.print("?>"); break; } case Node.COMMENT_NODE: { out.print("<!--"); out.print(node.getNodeValue()); out.print("-->"); break; } default: { out.print(normalize(node.getNodeValue())); break; } } out.flush(); }