Java tutorial
//package com.java2s; /******************************************************************************* * Copyright (c) 2003, 2011 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - Initial API and implementation *******************************************************************************/ import java.io.*; import org.w3c.dom.*; public class Main { public static byte[] getContents(Document document) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); try { print(new PrintStream(out, true, "UTF-8"), document); return out.toByteArray(); } catch (Exception ex) { throw new IOException(ex.getLocalizedMessage()); } finally { if (out != null) try { out.close(); } catch (Exception e) { // ignore } } } protected static void print(PrintStream out, Node node) { if (node == null) return; 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(); } protected static String getDocumentTypeData(DocumentType doctype) { String data = doctype.getName(); if (doctype.getPublicId() != null) { data += " PUBLIC \"" + doctype.getPublicId() + "\""; String systemId = doctype.getSystemId(); if (systemId == null) systemId = ""; data += " \"" + systemId + "\""; } else data += " SYSTEM \"" + doctype.getSystemId() + "\""; return data; } protected static String normalize(String s) { StringBuffer stringbuffer = new StringBuffer(); int i = s == null ? 0 : s.length(); for (int j = 0; j < i; j++) { char c = s.charAt(j); switch (c) { case 60: /* '<' */ stringbuffer.append("<"); break; case 62: /* '>' */ stringbuffer.append(">"); break; case 38: /* '&' */ stringbuffer.append("&"); break; case 34: /* '"' */ stringbuffer.append("""); break; case 10: /* '\n' */ case 13: /* '\r' */ default: stringbuffer.append(c); break; } } return stringbuffer.toString(); } /** * Get the value of this node. Will return "" instead of null. * @return java.lang.String * @param node org.w3c.dom.Node */ public static String getNodeValue(Node node) { NodeList nodeList = node.getChildNodes(); int length = nodeList.getLength(); for (int i = 0; i < length; i++) { Node n = nodeList.item(i); if (n instanceof Text) { Text t = (Text) n; return t.getNodeValue(); } } return ""; } public static String toString(Document document) { PrintStream out = null; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(2048); out = new PrintStream(baos); print(out, document); return new String(baos.toByteArray(), "UTF-8"); } catch (Exception ex) { // ignore } finally { if (out != null) try { out.close(); } catch (Exception e) { // ignore } } return null; } }