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
/** * returns the Node Type As String/*www . j a v a2s. c om*/ * @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
/** * /*from w w w . j a v a 2 s .co m*/ * @param currentNode * @param tagName * @param attributeValue * @return */ public static String getTextContentByElementNameANDAttributeValue(Node currentNode, String tagName, String attributeValue) { String result = ""; NodeList childNodeList = currentNode.getChildNodes(); for (int i = 0; i < childNodeList.getLength(); i++) { Node childNode = childNodeList.item(i); switch (childNode.getNodeType()) { case Node.DOCUMENT_NODE: break; case Node.ELEMENT_NODE: Element childElement = (Element) childNodeList.item(i); // logger.debug("childElement name : " + childElement.getTagName()); if (childElement != null && childElement.getNodeName().equals(tagName)) { NamedNodeMap attributes = childElement.getAttributes(); for (int j = 0; j < attributes.getLength(); j++) { Node current = attributes.item(j); if (current.getNodeName().equals("type") && current.getNodeValue().equals(attributeValue)) { result = childElement.getTextContent(); break; } } } case Node.TEXT_NODE: // logger.debug("textElement name : " + currentNode.getNodeValue()); break; case Node.COMMENT_NODE: break; case Node.PROCESSING_INSTRUCTION_NODE: break; case Node.ENTITY_REFERENCE_NODE: break; case Node.DOCUMENT_TYPE_NODE: break; } } return result; }
From source file:Main.java
@SuppressWarnings("null") public static void copyInto(Node src, Node dest) throws DOMException { Document factory = dest.getOwnerDocument(); //Node start = src; Node parent = null;//w w w.jav a2 s . com 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()) { ((Attr) 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; } else if (parent == null) { place = null; } else { // advance place = place.getNextSibling(); while (place == null && parent != null && dest != null) { place = parent.getNextSibling(); parent = parent.getParentNode(); dest = dest.getParentNode(); } } } }
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 ww w . jav a2 s . com*/ 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
/** * Searches the node for content of type Long. If non-long content is found, * it logs a warning and returns null.// w w w . j av a2s.c o m */ 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
/** * 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 w w. j a va2 s. c om*/ 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
/** * Gets Node type String for a given node type constant *//*from w ww . jav a2 s. com*/ public static String getNodeTypeStr(int nodeType) { switch (nodeType) { case Node.ATTRIBUTE_NODE: return "ATTRIBUTE_NODE "; case Node.CDATA_SECTION_NODE: return "CDATA_SECTION_NODE"; case Node.COMMENT_NODE: return "COMMENT_NODE"; case Node.DOCUMENT_FRAGMENT_NODE: return "DOCUMENT_FRAGMENT_NODE"; case Node.DOCUMENT_TYPE_NODE: return "DOCUMENT_TYPE_NODE"; case Node.ELEMENT_NODE: return "ELEMENT_NODE"; case Node.ENTITY_NODE: return "ENTITY_NODE"; case Node.ENTITY_REFERENCE_NODE: return "ENTITY_REFERENCE_NODE"; case Node.NOTATION_NODE: return "NOTATION_NODE"; case Node.PROCESSING_INSTRUCTION_NODE: return "PROCESSING_INSTRUCTION_NODE"; case Node.TEXT_NODE: return "TEXT_NODE"; case Node.DOCUMENT_NODE: return "DOCUMENT_NODE"; default: return "UN-INDENTIFIED NODE"; } }
From source file:Main.java
public String getNodeType(Node node) { String type;/*from w w w . ja v a 2s . com*/ 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
public static void copyInto(Node src, Node dest) throws DOMException { Document factory = dest.getOwnerDocument(); //Node start = src; Node parent = null;/* w ww . 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
/** 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 ww . j a v a 2s . c o 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('>'); } }