List of usage examples for org.w3c.dom Node ENTITY_REFERENCE_NODE
short ENTITY_REFERENCE_NODE
To view the source code for org.w3c.dom Node ENTITY_REFERENCE_NODE.
Click Source Link
EntityReference
. From source file:Main.java
/** * Collapses a list of CDATASection, Text, and predefined EntityReference * nodes into a single string. If the list contains other types of nodes, * those other nodes are ignored./* w ww . j a va 2 s . c om*/ */ public static String getText(NodeList nodeList) { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); switch (node.getNodeType()) { case Node.CDATA_SECTION_NODE: case Node.TEXT_NODE: buffer.append(node.getNodeValue()); break; case Node.ENTITY_REFERENCE_NODE: if (node.getNodeName().equals("amp")) buffer.append('&'); else if (node.getNodeName().equals("lt")) buffer.append('<'); else if (node.getNodeName().equals("gt")) buffer.append('>'); else if (node.getNodeName().equals("apos")) buffer.append('\''); else if (node.getNodeName().equals("quot")) buffer.append('"'); // Any other entity references are ignored break; default: // All other nodes are ignored } } return buffer.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 w ww.j a v a 2 s. c o m*/ return null; } }
From source file:Main.java
/** * @param node//from www .j a v a 2s .co m */ public static void displayNodeInfo(Node node) { switch (node.getNodeType()) { case Node.DOCUMENT_NODE: System.out.println("Document Node "); break; case Node.ELEMENT_NODE: System.out.println("Element Node: " + node.getNodeName()); break; case Node.TEXT_NODE: System.out.println("Text Node: " + node.getNodeName()); break; case Node.CDATA_SECTION_NODE: System.out.println("CDATA Section Node: "); break; case Node.COMMENT_NODE: System.out.println("Comment Node "); break; case Node.PROCESSING_INSTRUCTION_NODE: System.out.println("Processing Instruction Node "); break; case Node.ENTITY_REFERENCE_NODE: System.out.println("Entity Reference Node "); break; case Node.DOCUMENT_TYPE_NODE: System.out.println("Document Type Node "); break; } }
From source file:Main.java
/**prints the document tree * @param node node to start at//from www . jav a 2 s. c om * @param ident amount of indention*/ public static void printTree(Node node, int ident) { if (node == null) return; NodeList children; System.out.print("Node: " + node.getNodeName() + " "); switch (node.getNodeType()) { case Node.DOCUMENT_NODE: System.out.println("Document Node"); break; case Node.ELEMENT_NODE: System.out.println("Element Node"); break; case Node.TEXT_NODE: System.out.println("->" + node.getNodeValue().trim() + "<-"); break; case Node.CDATA_SECTION_NODE: System.out.println("CData Node"); break; case Node.PROCESSING_INSTRUCTION_NODE: System.out.println("Proposing Instruction Node"); break; case Node.ENTITY_REFERENCE_NODE: System.out.println("Entity Node"); break; case Node.DOCUMENT_TYPE_NODE: System.out.println("Document Node"); break; default: } for (int j = 0; j < 2 * ident; j++) System.out.print(" "); System.out.println("It has the following Children"); children = node.getChildNodes(); if (children != null) { for (int i = 0; i < children.getLength(); i++) { for (int j = 0; j < ident; j++) System.out.print(" "); System.out.print("Child " + ident + "." + i + " = "); printNodeType(children.item(i), ident + 1); } System.out.println(); } }
From source file:Main.java
public static String getText(final Node node) { final StringBuilder result = new StringBuilder(); if (!node.hasChildNodes()) return ""; final NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node subnode = list.item(i); if (subnode.getNodeType() == Node.TEXT_NODE) { result.append(subnode.getNodeValue()); } else if (subnode.getNodeType() == Node.CDATA_SECTION_NODE) { result.append(subnode.getNodeValue()); } else if (subnode.getNodeType() == Node.ENTITY_REFERENCE_NODE) { // Recurse into the subtree for text // (and ignore comments) result.append(getText(subnode)); }/*from w ww. j a v a 2 s .c o m*/ } return result.toString(); }
From source file:Main.java
/** * Static helper function to get the element data of the specified node. * /*from w w w . j av a 2 s .c o m*/ * @param node * the node where the text data resides; may be <code>null</code> * in which case this funtion will return "" * * @return the complete text of the specified node, or an empty string if * the node has no text or is <code>null</code> */ public static String getElementData(final Node node) { StringBuffer ret = new StringBuffer(); if (node != null) { Node text; for (text = node.getFirstChild(); text != null; text = text.getNextSibling()) { /** * the item's value is in one or more text nodes which are its * immediate children */ if (text.getNodeType() == Node.TEXT_NODE || text.getNodeType() == Node.CDATA_SECTION_NODE) { ret.append(text.getNodeValue()); } else { if (text.getNodeType() == Node.ENTITY_REFERENCE_NODE) { ret.append(getElementData(text)); } } } } return ret.toString(); }
From source file:Main.java
/** * based on public Java5 javadoc of org.w3c.dom.Node.getTextContent method *///from ww w.j ava 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 String getText(Node node) { StringBuffer result = new StringBuffer(); if (!node.hasChildNodes()) return ""; NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) {//from www . j a va 2 s . c o m Node subnode = list.item(i); if (subnode.getNodeType() == Node.TEXT_NODE) { result.append(subnode.getNodeValue()); } else if (subnode.getNodeType() == Node.CDATA_SECTION_NODE) { result.append(subnode.getNodeValue()); } else if (subnode.getNodeType() == Node.ENTITY_REFERENCE_NODE) { // Recurse into the subtree for text // (and ignore comments) result.append(getText(subnode)); } } return result.toString(); }
From source file:Main.java
/** * Convert a node type to a string. For debug purpose only. * //from w w w .j a v a 2 s. c o m * @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); }
From source file:Main.java
private 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 w ww . jav a2 s .c o m*/ return null; } }