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:MainClass.java
public void processNode(Node node, String spacer) throws IOException { if (node == null) return;/*from w ww . ja va2 s. c om*/ switch (node.getNodeType()) { case Node.ELEMENT_NODE: String name = node.getNodeName(); System.out.print(spacer + "<" + name); NamedNodeMap nnm = node.getAttributes(); for (int i = 0; i < nnm.getLength(); i++) { Node current = nnm.item(i); System.out.print(" " + current.getNodeName() + "= " + current.getNodeValue()); } System.out.print(">"); NodeList nl = node.getChildNodes(); if (nl != null) { for (int i = 0; i < nl.getLength(); i++) { processNode(nl.item(i), ""); } } System.out.println(spacer + "</" + name + ">"); break; case Node.TEXT_NODE: System.out.print(node.getNodeValue()); break; case Node.CDATA_SECTION_NODE: System.out.print("" + node.getNodeValue() + ""); break; case Node.ENTITY_REFERENCE_NODE: System.out.print("&" + node.getNodeName() + ";"); break; case Node.ENTITY_NODE: System.out.print("<ENTITY: " + node.getNodeName() + "> </" + node.getNodeName() + "/>"); break; case Node.DOCUMENT_NODE: NodeList nodes = node.getChildNodes(); if (nodes != null) { for (int i = 0; i < nodes.getLength(); i++) { processNode(nodes.item(i), ""); } } break; case Node.DOCUMENT_TYPE_NODE: DocumentType docType = (DocumentType) node; System.out.print("<!DOCTYPE " + docType.getName()); if (docType.getPublicId() != null) { System.out.print(" PUBLIC " + docType.getPublicId() + " "); } else { System.out.print(" SYSTEM "); } System.out.println(" " + docType.getSystemId() + ">"); break; default: break; } }
From source file:Main.java
/** Prints the specified node, recursively. * * @param node Node to be printed//from ww w . ja v a2 s.com */ public static void print(Node node) { // is there anything to do? if (node == null) { return; } System.out.println(""); int type = node.getNodeType(); switch (type) { // print document case Node.DOCUMENT_NODE: { /* if (!canonical) { if (Encoding.equalsIgnoreCase("DEFAULT")) Encoding = "UTF-8"; else if(Encoding.equalsIgnoreCase("Unicode")) Encoding = "UTF-16"; else Encoding = MIME2Java.reverse(Encoding); out.println("<?xml version=\"1.0\" encoding=\"" + Encoding + "\"?>"); } */ print(((Document) node).getDocumentElement()); out.flush(); break; } // print element with attributes case Node.ELEMENT_NODE: { out.print('<'); out.print(node.getNodeName()); Attr attrs[] = sortAttributes(node.getAttributes()); for (int i = 0; i < attrs.length; i++) { Attr attr = attrs[i]; out.print(' '); out.print(attr.getNodeName()); out.print("=\""); out.print(normalize(attr.getNodeValue())); out.print('"'); } out.print('>'); NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) { print(children.item(i)); } } break; } // handle entity reference nodes case Node.ENTITY_REFERENCE_NODE: { if (canonical) { NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) { print(children.item(i)); } } } else { out.print('&'); out.print(node.getNodeName()); out.print(';'); } break; } // print cdata sections case Node.CDATA_SECTION_NODE: { if (canonical) { out.print(normalize(node.getNodeValue())); } else { out.print("<![CDATA["); out.print(node.getNodeValue()); out.print("]]>"); } break; } // print DocumentType sections case Node.DOCUMENT_TYPE_NODE: { out.print("<!DOCTYPE "); out.print(((DocumentType) node).getName()); out.print(" SYSTEM "); out.print(((DocumentType) node).getSystemId()); out.print(">"); break; } // print text case Node.TEXT_NODE: { out.print(normalize(node.getNodeValue())); break; } // print processing instruction case Node.PROCESSING_INSTRUCTION_NODE: { out.print("<?"); out.print(node.getNodeName()); String data = node.getNodeValue(); if (data != null && data.length() > 0) { out.print(' '); out.print(data); } out.print("?>"); break; } } if (type == Node.ELEMENT_NODE) { out.print("</"); out.print(node.getNodeName()); out.print('>'); } out.flush(); }
From source file:DOMDump.java
private static void dumpLoop(Node node, String indent) { switch (node.getNodeType()) { case Node.CDATA_SECTION_NODE: System.out.println(indent + "CDATA_SECTION_NODE"); break;/* w w w.java 2s . c o m*/ 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.print(indent + "TEXT_NODE"); System.out.println(" : " + node.getTextContent()); 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
protected static void print(PrintStream out, Node node) { if (node == null) return;/*from ww w .j a v a 2s .c om*/ 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(); }
From source file:Main.java
/** * Looks for a text child node and returns its value. * * @param tag - XML element/* w w w. ja v a2s.c om*/ * @return - the text String of the tag */ public static String getText(final Element tag) { if (tag == null) return null; NodeList lst = tag.getChildNodes(); StringBuffer buf = new StringBuffer(); for (int Index = 0, Cnt = lst.getLength(); Index < Cnt; Index++) { if (lst.item(Index).getNodeType() == Node.ENTITY_REFERENCE_NODE) { buf.append(lst.item(Index).getChildNodes().item(0).getNodeValue()); } if ((lst.item(Index).getNodeType() == Node.TEXT_NODE) || (lst.item(Index).getNodeType() == Node.CDATA_SECTION_NODE)) { buf.append(lst.item(Index).getNodeValue()); } } if (buf.length() == 0) return null; else return buf.toString(); }
From source file:Main.java
/** * @param node/* w ww . j a v a2s . c om*/ * @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
/** * Copies the source tree into the specified place in a destination * tree. The source node and its children are appended as children * of the destination node./* w ww .j a va 2s . co m*/ * <p> * <em>Note:</em> This is an iterative implementation. */ public static void copyInto(Node src, Node dest) throws DOMException { // get node factory Document factory = dest.getOwnerDocument(); boolean domimpl = factory instanceof DocumentImpl; // placement variables Node start = src; Node parent = src; 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()) { ((AttrImpl) 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; } // advance else { place = place.getNextSibling(); while (place == null && parent != start) { place = parent.getNextSibling(); parent = parent.getParentNode(); dest = dest.getParentNode(); } } } }
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. * //from ww w. j a v a2 s. c om * @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
/**prints the type of the input node * @param node node to print type of/*from w w w .j a v a2 s . c o m*/ * @param ident amount to indent*/ public static void printNodeType(Node node, int ident) { 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"); for (int j = 0; j < 2 * ident; j++) System.out.print(" "); System.out.println("It has the following Children"); NodeList 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(); } 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: } }
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 . ja v a 2 s . 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 + " "); }