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
protected static void print(PrintStream out, Node node) { if (node == null) return;//from ww w . j a va2 s . 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
/** * Print a Node tree recursively./*from www . jav a2 s . c o m*/ * @param node A DOM tree Node * @return An xml String representation of the DOM tree. */ public static String print(Node node) { if (node == null) { return null; } StringBuffer xml = new StringBuffer(100); int type = node.getNodeType(); switch (type) { // print element with attributes case Node.ELEMENT_NODE: { xml.append('<'); xml.append(node.getNodeName()); NamedNodeMap attrs = node.getAttributes(); int length = attrs.getLength(); ; for (int i = 0; i < length; i++) { Attr attr = (Attr) attrs.item(i); xml.append(' '); xml.append(attr.getNodeName()); xml.append("=\""); //xml.append(normalize(attr.getNodeValue())); xml.append(attr.getNodeValue()); xml.append('"'); } xml.append('>'); NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) { xml.append(print(children.item(i))); } } break; } // handle entity reference nodes case Node.ENTITY_REFERENCE_NODE: { NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) { xml.append(print(children.item(i))); } } break; } // print cdata sections case Node.CDATA_SECTION_NODE: { xml.append("<![CDATA["); xml.append(node.getNodeValue()); xml.append("]]>"); break; } // print text case Node.TEXT_NODE: { //xml.append(normalize(node.getNodeValue())); xml.append(node.getNodeValue()); break; } // print processing instruction case Node.PROCESSING_INSTRUCTION_NODE: { xml.append("<?"); xml.append(node.getNodeName()); String data = node.getNodeValue(); if ((data != null) && (data.length() > 0)) { xml.append(' '); xml.append(data); } xml.append("?>"); break; } } if (type == Node.ELEMENT_NODE) { xml.append("</"); xml.append(node.getNodeName()); xml.append('>'); } return xml.toString(); }
From source file:fr.gouv.finances.dgfip.xemelios.utils.XmlUtils.java
public static String getXmlDataSubstituteNode(Node node, String substituteWith, String substituteInWhat) { StringBuilder sb = new StringBuilder(); switch (node.getNodeType()) { case Node.COMMENT_NODE: case Node.ENTITY_NODE: case Node.ENTITY_REFERENCE_NODE: case Node.NOTATION_NODE: case Node.PROCESSING_INSTRUCTION_NODE: break;/*w w w. jav a 2 s . c o m*/ case Node.DOCUMENT_NODE: case Node.DOCUMENT_FRAGMENT_NODE: case Node.ELEMENT_NODE: { String nodeName = node.getNodeName(); if (!substituteInWhat.equals(nodeName)) { sb.append("<").append(nodeName); StringBuilder attrs = new StringBuilder(); StringBuilder children = new StringBuilder(); NamedNodeMap nnm = node.getAttributes(); if (nnm != null) { for (int i = 0; i < nnm.getLength(); i++) { Node attr = nnm.item(i); attrs.append(" ").append(getXmlDataSubstituteNode(attr, substituteWith, substituteInWhat)); } } NodeList nl = node.getChildNodes(); if (nl != null) { for (int i = 0; i < nl.getLength(); i++) { Node child = nl.item(i); if (child.getNodeType() == Node.ATTRIBUTE_NODE) { attrs.append(" ") .append(getXmlDataSubstituteNode(child, substituteWith, substituteInWhat)); } else { children.append(getXmlDataSubstituteNode(child, substituteWith, substituteInWhat)); } } } sb.append(attrs.toString()); if (children.length() > 0) { sb.append(">").append(children.toString()).append("</").append(nodeName).append(">"); } else { sb.append("/>"); } } else { sb.append(substituteWith); } break; } case Node.ATTRIBUTE_NODE: { sb.append(node.getNodeName()).append("=\"").append(StringEscapeUtils.escapeXml(node.getNodeValue())) .append("\""); break; } case Node.CDATA_SECTION_NODE: { sb.append("<![CDATA[").append(StringEscapeUtils.escapeXml(node.getNodeValue())).append("]]>"); } case Node.TEXT_NODE: { sb.append(StringEscapeUtils.escapeXml(node.getNodeValue())); } } return sb.toString(); }
From source file:MainClass.java
private void dumpLoop(Node node, String indent) { switch (node.getNodeType()) { case Node.CDATA_SECTION_NODE: System.out.println(indent + "CDATA_SECTION_NODE"); break;// www .j a va 2s .co 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.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: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 . j ava2s . co 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.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:Main.java
/** * Generates XPath expression with the option of the Node values appended * * @param node the Node whose XPath is to be found * @param ignoreWhitespace the flag to indicate if Whitespace will be ignored * @param includeValues the flag to indicate if Node values will be included * @param noIndex the flag to indicate if Node indexes are included * @return the XPath string representation of the Node *///from w w w.java 2 s . co m public static String generateXPath(Node node, boolean ignoreWhitespace, boolean includeValues, boolean noIndex) { boolean noValues = !includeValues; if (node == null) return ""; Node parent = node.getParentNode(); int index = noIndex ? 0 : getXPathNodeIndex(node, ignoreWhitespace); String indexStr = ""; if (index > 0) indexStr = "[" + Integer.toString(index) + "]"; //printNode(node); //printNode(parent); if (node.getNodeType() == Node.DOCUMENT_NODE) { // return only the blank String, since all the other types are preceded with / return ""; } else if (node.getNodeType() == Node.TEXT_NODE) { return generateXPath(parent, ignoreWhitespace, noValues, noIndex) + (noValues ? node.getNodeValue() + indexStr : "/TEXT(" + node.getNodeValue() + ")" + indexStr); } else if (node.getNodeType() == Node.ELEMENT_NODE) { return generateXPath(parent, ignoreWhitespace, noValues, noIndex) + "/" + node.getNodeName() + indexStr; } else if (node.getNodeType() == Node.COMMENT_NODE) { return generateXPath(parent, ignoreWhitespace, noValues, noIndex) + (noValues ? node.getNodeValue() + indexStr : "/COMMENT(" + node.getNodeValue() + ")" + indexStr); } else if (node.getNodeType() == Node.ENTITY_REFERENCE_NODE) { return generateXPath(parent, ignoreWhitespace, noValues, noIndex) + (noValues ? node.getNodeValue() + indexStr : "/EntityReference(" + node.getNodeValue() + ")" + indexStr); } else if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) { return generateXPath(parent, ignoreWhitespace, noValues, noIndex) + (noValues ? node.getNodeValue() + indexStr : "/PI(" + node.getNodeValue() + ")" + indexStr); } else if (node.getNodeType() == Node.ATTRIBUTE_NODE) { return generateXPath(((Attr) node).getOwnerElement(), ignoreWhitespace, noValues, noIndex) + "/'@" + node.getNodeName() + (noValues ? "" : "=" + node.getNodeValue()) + "]"; } else if (node.getNodeType() == Node.DOCUMENT_TYPE_NODE) { return generateXPath(parent, ignoreWhitespace, noValues, noIndex) + (noValues ? node.getNodeValue() : "/DOCTYPE(" + node.getNodeName() + ")"); } else if (node.getNodeType() == Node.CDATA_SECTION_NODE) { return generateXPath(parent, ignoreWhitespace, noValues, noIndex) + (noValues ? node.getNodeValue() : "/CDATA(" + node.getNodeName() + ")"); } // Wont reach this far but just in case return ""; }
From source file:Main.java
public final static Class<? extends Node> toClass(final short nodeType) { switch (nodeType) { case Node.ATTRIBUTE_NODE: return Attr.class; case Node.CDATA_SECTION_NODE: return CDATASection.class; case Node.COMMENT_NODE: return Comment.class; case Node.DOCUMENT_FRAGMENT_NODE: return DocumentFragment.class; case Node.DOCUMENT_NODE: return Document.class; case Node.DOCUMENT_TYPE_NODE: return DocumentType.class; case Node.ELEMENT_NODE: return Element.class; case Node.ENTITY_NODE: return Entity.class; case Node.ENTITY_REFERENCE_NODE: return EntityReference.class; case Node.NOTATION_NODE: return Notation.class; case Node.PROCESSING_INSTRUCTION_NODE: return ProcessingInstruction.class; case Node.TEXT_NODE: return Text.class; }// w ww . j av a 2 s.c o m throw new RuntimeException("Unrecognized node type " + nodeType); }
From source file:XMLDocumentWriter.java
/** * Output the specified DOM Node object, printing it using the specified * indentation string//from w w w . j ava 2 s .com */ public void write(Node node, String indent) { // The output depends on the type of the node switch (node.getNodeType()) { case Node.DOCUMENT_NODE: { // If its a Document node Document doc = (Document) node; out.println(indent + "<?xml version='1.0'?>"); // Output header Node child = doc.getFirstChild(); // Get the first node while (child != null) { // Loop 'till no more nodes write(child, indent); // Output node child = child.getNextSibling(); // Get next node } break; } case Node.DOCUMENT_TYPE_NODE: { // It is a <!DOCTYPE> tag DocumentType doctype = (DocumentType) node; // Note that the DOM Level 1 does not give us information about // the the public or system ids of the doctype, so we can't output // a complete <!DOCTYPE> tag here. We can do better with Level 2. out.println("<!DOCTYPE " + doctype.getName() + ">"); break; } case Node.ELEMENT_NODE: { // Most nodes are Elements Element elt = (Element) node; out.print(indent + "<" + elt.getTagName()); // Begin start tag NamedNodeMap attrs = elt.getAttributes(); // Get attributes for (int i = 0; i < attrs.getLength(); i++) { // Loop through them Node a = attrs.item(i); out.print(" " + a.getNodeName() + "='" + // Print attr. name fixup(a.getNodeValue()) + "'"); // Print attr. value } out.println(">"); // Finish start tag String newindent = indent + " "; // Increase indent Node child = elt.getFirstChild(); // Get child while (child != null) { // Loop write(child, newindent); // Output child child = child.getNextSibling(); // Get next child } out.println(indent + "</" + // Output end tag elt.getTagName() + ">"); break; } case Node.TEXT_NODE: { // Plain text node Text textNode = (Text) node; String text = textNode.getData().trim(); // Strip off space if ((text != null) && text.length() > 0) // If non-empty out.println(indent + fixup(text)); // print text break; } case Node.PROCESSING_INSTRUCTION_NODE: { // Handle PI nodes ProcessingInstruction pi = (ProcessingInstruction) node; out.println(indent + "<?" + pi.getTarget() + " " + pi.getData() + "?>"); break; } case Node.ENTITY_REFERENCE_NODE: { // Handle entities out.println(indent + "&" + node.getNodeName() + ";"); break; } case Node.CDATA_SECTION_NODE: { // Output CDATA sections CDATASection cdata = (CDATASection) node; // Careful! Don't put a CDATA section in the program itself! out.println(indent + "<" + "![CDATA[" + cdata.getData() + "]]" + ">"); break; } case Node.COMMENT_NODE: { // Comments Comment c = (Comment) node; out.println(indent + "<!--" + c.getData() + "-->"); break; } default: // Hopefully, this won't happen too much! System.err.println("Ignoring node: " + node.getClass().getName()); break; } }
From source file:fr.gouv.finances.dgfip.xemelios.utils.TextWriter.java
private void writeNode(Writer writer, Node node) throws IOException { short type = node.getNodeType(); switch (type) { case Node.DOCUMENT_NODE: document(writer, (Document) node); break;/* w ww .j av a2 s . c o m*/ case Node.DOCUMENT_FRAGMENT_NODE: documentFragment(writer, (DocumentFragment) node); break; case Node.DOCUMENT_TYPE_NODE: documentType(writer, (DocumentType) node); break; case Node.ELEMENT_NODE: element(writer, (Element) node); break; case Node.ATTRIBUTE_NODE: attribute(writer, (Attr) node); break; case Node.ENTITY_REFERENCE_NODE: entityReference(writer, (EntityReference) node); break; case Node.ENTITY_NODE: entity(writer, (Entity) node); break; case Node.NOTATION_NODE: notation(writer, (Notation) node); break; case Node.PROCESSING_INSTRUCTION_NODE: procInst(writer, (ProcessingInstruction) node); break; case Node.TEXT_NODE: text(writer, (Text) node); break; case Node.CDATA_SECTION_NODE: cDataSection(writer, (CDATASection) node); break; case Node.COMMENT_NODE: comment(writer, (Comment) node); break; } }
From source file:DOM2SAX.java
/** * Writes a node using the given writer. * @param node node to serialize// www. ja v a2s .co m * @throws SAXException In case of a problem while writing XML */ private void writeNode(Node node) throws SAXException { if (node == null) { return; } switch (node.getNodeType()) { case Node.ATTRIBUTE_NODE: // handled by ELEMENT_NODE case Node.DOCUMENT_FRAGMENT_NODE: case Node.DOCUMENT_TYPE_NODE: case Node.ENTITY_NODE: case Node.ENTITY_REFERENCE_NODE: case Node.NOTATION_NODE: // These node types are ignored!!! break; case Node.CDATA_SECTION_NODE: final String cdata = node.getNodeValue(); if (lexicalHandler != null) { lexicalHandler.startCDATA(); contentHandler.characters(cdata.toCharArray(), 0, cdata.length()); lexicalHandler.endCDATA(); } else { // in the case where there is no lex handler, we still // want the text of the cdate to make its way through. contentHandler.characters(cdata.toCharArray(), 0, cdata.length()); } break; case Node.COMMENT_NODE: // should be handled!!! if (lexicalHandler != null) { final String value = node.getNodeValue(); lexicalHandler.comment(value.toCharArray(), 0, value.length()); } break; case Node.DOCUMENT_NODE: contentHandler.startDocument(); Node next = node.getFirstChild(); while (next != null) { writeNode(next); next = next.getNextSibling(); } contentHandler.endDocument(); break; case Node.ELEMENT_NODE: String prefix; List pushedPrefixes = new java.util.ArrayList(); final AttributesImpl attrs = new AttributesImpl(); final NamedNodeMap map = node.getAttributes(); final int length = map.getLength(); // Process all namespace declarations for (int i = 0; i < length; i++) { final Node attr = map.item(i); final String qnameAttr = attr.getNodeName(); // Ignore everything but NS declarations here if (qnameAttr.startsWith(XMLNS_PREFIX)) { final String uriAttr = attr.getNodeValue(); final int colon = qnameAttr.lastIndexOf(':'); prefix = (colon > 0) ? qnameAttr.substring(colon + 1) : EMPTYSTRING; if (startPrefixMapping(prefix, uriAttr)) { pushedPrefixes.add(prefix); } } } // Process all other attributes for (int i = 0; i < length; i++) { final Node attr = map.item(i); final String qnameAttr = attr.getNodeName(); // Ignore NS declarations here if (!qnameAttr.startsWith(XMLNS_PREFIX)) { final String uriAttr = attr.getNamespaceURI(); // Uri may be implicitly declared if (uriAttr != null) { final int colon = qnameAttr.lastIndexOf(':'); prefix = (colon > 0) ? qnameAttr.substring(0, colon) : EMPTYSTRING; if (startPrefixMapping(prefix, uriAttr)) { pushedPrefixes.add(prefix); } } // Add attribute to list attrs.addAttribute(attr.getNamespaceURI(), getLocalName(attr), qnameAttr, "CDATA", attr.getNodeValue()); } } // Now process the element itself final String qname = node.getNodeName(); final String uri = node.getNamespaceURI(); final String localName = getLocalName(node); // Uri may be implicitly declared if (uri != null) { final int colon = qname.lastIndexOf(':'); prefix = (colon > 0) ? qname.substring(0, colon) : EMPTYSTRING; if (startPrefixMapping(prefix, uri)) { pushedPrefixes.add(prefix); } } // Generate SAX event to start element contentHandler.startElement(uri, localName, qname, attrs); // Traverse all child nodes of the element (if any) next = node.getFirstChild(); while (next != null) { writeNode(next); next = next.getNextSibling(); } // Generate SAX event to close element contentHandler.endElement(uri, localName, qname); // Generate endPrefixMapping() for all pushed prefixes final int nPushedPrefixes = pushedPrefixes.size(); for (int i = 0; i < nPushedPrefixes; i++) { endPrefixMapping((String) pushedPrefixes.get(i)); } break; case Node.PROCESSING_INSTRUCTION_NODE: contentHandler.processingInstruction(node.getNodeName(), node.getNodeValue()); break; case Node.TEXT_NODE: final String data = node.getNodeValue(); contentHandler.characters(data.toCharArray(), 0, data.length()); break; default: //nop } }