List of usage examples for org.w3c.dom Node COMMENT_NODE
short COMMENT_NODE
To view the source code for org.w3c.dom Node COMMENT_NODE.
Click Source Link
Comment
. From source file:Main.java
private static String mergeTextContent(final NodeList nodes) { StringBuilder buf = new StringBuilder(); for (int i = 0; i < nodes.getLength(); i++) { Node n = nodes.item(i);//from w w w.ja va 2 s.c o m final String text; switch (n.getNodeType()) { case Node.COMMENT_NODE: case Node.PROCESSING_INSTRUCTION_NODE: // ignore comments when merging text = null; break; default: text = getTextContent(n); break; } if (text != null) { buf.append(text); } } return buf.toString(); }
From source file:Main.java
public static String getTextContent(final Node node) { switch (node.getNodeType()) { case Node.ELEMENT_NODE: case Node.ATTRIBUTE_NODE: case Node.ENTITY_NODE: case Node.ENTITY_REFERENCE_NODE: case Node.DOCUMENT_FRAGMENT_NODE: return mergeTextContent(node.getChildNodes()); case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: case Node.COMMENT_NODE: case Node.PROCESSING_INSTRUCTION_NODE: return node.getNodeValue(); case Node.DOCUMENT_NODE: case Node.DOCUMENT_TYPE_NODE: case Node.NOTATION_NODE: default:/*from ww w . j av a2 s.c om*/ return null; } }
From source file:Main.java
static public String getDocumentComment(Document doc) { String docComment = null;/*from w w w . j ava2 s.c o m*/ try { NodeList childs = doc.getChildNodes(); for (int i = 0; i < childs.getLength(); i++) { Node child = childs.item(i); if (child.getNodeType() == Node.COMMENT_NODE) { docComment = child.getNodeValue(); } } return docComment; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * returns the Node Type As String//from w w w . j a va 2 s . co m * @param node * @param cftype * @return */ public static String getTypeAsString(Node node, boolean cftype) { String suffix = cftype ? "" : "_NODE"; switch (node.getNodeType()) { case Node.ATTRIBUTE_NODE: return "ATTRIBUTE" + suffix; case Node.CDATA_SECTION_NODE: return "CDATA_SECTION" + suffix; case Node.COMMENT_NODE: return "COMMENT" + suffix; case Node.DOCUMENT_FRAGMENT_NODE: return "DOCUMENT_FRAGMENT" + suffix; case Node.DOCUMENT_NODE: return "DOCUMENT" + suffix; case Node.DOCUMENT_TYPE_NODE: return "DOCUMENT_TYPE" + suffix; case Node.ELEMENT_NODE: return "ELEMENT" + suffix; case Node.ENTITY_NODE: return "ENTITY" + suffix; case Node.ENTITY_REFERENCE_NODE: return "ENTITY_REFERENCE" + suffix; case Node.NOTATION_NODE: return "NOTATION" + suffix; case Node.PROCESSING_INSTRUCTION_NODE: return "PROCESSING_INSTRUCTION" + suffix; case Node.TEXT_NODE: return "TEXT" + suffix; default: return "UNKNOW" + suffix; } }
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 w ww .ja va2 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
/** * based on public Java5 javadoc of org.w3c.dom.Node.getTextContent method *///from w ww . jav a 2 s. c o m public static String getTextContent(Node node) { switch (node.getNodeType()) { case Node.ELEMENT_NODE: case Node.ATTRIBUTE_NODE: case Node.ENTITY_NODE: case Node.ENTITY_REFERENCE_NODE: case Node.DOCUMENT_FRAGMENT_NODE: return mergeTextContent(node.getChildNodes()); case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: case Node.COMMENT_NODE: case Node.PROCESSING_INSTRUCTION_NODE: return node.getNodeValue(); case Node.DOCUMENT_NODE: case Node.DOCUMENT_TYPE_NODE: case Node.NOTATION_NODE: default: return null; } }
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;//www .ja va 2s. c om 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
static void printElement(Element element, String indent) { System.out.println("Element '" + element.getNodeName() + "'"); NodeList children = element.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); switch (child.getNodeType()) { case Node.ELEMENT_NODE: printElement((Element) child, indent + "\t"); break; case Node.ATTRIBUTE_NODE: Attr attr = (Attr) child; System.out.println("\tAttribute: '" + attr.getName() + "' = '" + attr.getValue() + "'"); break; case Node.COMMENT_NODE: Comment comment = (Comment) child; System.out.println("\tComment: '" + comment.getData() + "'"); break; case Node.CDATA_SECTION_NODE: CharacterData cdata = (CharacterData) child; System.out.println("\tCDatat: '" + cdata.getData() + "'"); break; case Node.TEXT_NODE: Text text = (Text) child; System.out.println("\tText: '" + text.getData() + "'"); break; default://from w w w . j av a 2 s.c o m System.out.println("\tUnknown node type: '" + child.getNodeType() + "'"); break; } } }
From source file:Main.java
@SuppressWarnings("fallthrough") private static void getSetRec(final Node rootNode, final Set<Node> result, final Node exclude, final boolean com) { if (rootNode == exclude) { return;//from w w w .j a v a2 s . c o m } switch (rootNode.getNodeType()) { case Node.ELEMENT_NODE: result.add(rootNode); Element el = (Element) rootNode; if (el.hasAttributes()) { NamedNodeMap nl = el.getAttributes(); for (int i = 0; i < nl.getLength(); i++) { result.add(nl.item(i)); } } //no return keep working 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); } }
From source file:Main.java
/** * Convert a node type to a string. For debug purpose only. * // w ww . j ava2 s . c om * @param nodeType * the node type * @return the string * @throws Exception * the exception */ public static String nodeTypeToString(short nodeType) throws Exception { if (nodeType == Node.ELEMENT_NODE) return "ELEMENT_NODE"; if (nodeType == Node.ATTRIBUTE_NODE) return "ATTRIBUTE_NODE"; if (nodeType == Node.TEXT_NODE) return "TEXT_NODE"; if (nodeType == Node.CDATA_SECTION_NODE) return "CDATA_SECTION_NODE"; if (nodeType == Node.ENTITY_REFERENCE_NODE) return "ENTITY_REFERENCE_NODE"; if (nodeType == Node.ENTITY_NODE) return "ENTITY_NODE"; if (nodeType == Node.PROCESSING_INSTRUCTION_NODE) return "PROCESSING_INSTRUCTION_NODE"; if (nodeType == Node.COMMENT_NODE) return "COMMENT_NODE"; if (nodeType == Node.DOCUMENT_NODE) return "DOCUMENT_NODE"; if (nodeType == Node.DOCUMENT_TYPE_NODE) return "DOCUMENT_TYPE_NODE"; if (nodeType == Node.DOCUMENT_FRAGMENT_NODE) return "DOCUMENT_FRAGMENT_NODE"; if (nodeType == Node.NOTATION_NODE) return "NOTATION_NODE"; if (nodeType == Node.DOCUMENT_POSITION_DISCONNECTED) return "DOCUMENT_POSITION_DISCONNECTED"; if (nodeType == Node.DOCUMENT_POSITION_PRECEDING) return "DOCUMENT_POSITION_PRECEDING"; if (nodeType == Node.DOCUMENT_POSITION_FOLLOWING) return "DOCUMENT_POSITION_FOLLOWING"; if (nodeType == Node.DOCUMENT_POSITION_CONTAINS) return "DOCUMENT_POSITION_CONTAINS"; if (nodeType == Node.DOCUMENT_POSITION_CONTAINED_BY) return "DOCUMENT_POSITION_CONTAINED_BY"; if (nodeType == Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC) return "DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC"; throw new Exception("Unknown value : " + nodeType); }