Java tutorial
//package com.java2s; /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. * */ import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.StringWriter; import javax.xml.namespace.QName; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import com.sun.org.apache.xml.internal.serialize.OutputFormat; import com.sun.org.apache.xml.internal.serialize.XMLSerializer; public class Main { /** * This is a convenience method that calls toFile(Node, File, boolean) * with the last parameter set to "true" (to add the standard XML * header to the new XML file). * * @see #toFile(Node, File, boolean) */ public static void toFile(final Node xml, final File file) throws IOException { toFile(xml, file, true); } /** * Serializes the given XML sub-tree to the given file. * * @param xml * The XML tree to serialize - must be a Document or Element. * @param file * The file to write to. If it already exists, its contents will * be overwritten; if it does not, it will be created. * @param printHeader * True if the standard XML document header should be added to * the top of the file. * @throws IOException * <ul> * <li>If there is an error creating, opening, or while writing * to the file.</li> * </ul> * @see #toString(Node, boolean) */ public static void toFile(final Node xml, final File file, final boolean printHeader) throws IOException { final String xmlString = toString(xml, printHeader); final FileWriter writer = new FileWriter(file); writer.write(xmlString); writer.close(); } /** * This is a convenience method that serializes the given XML tree * with the XML header and indentation; it is the equivalent of * calling toString(Node, boolean) with the last parameter set to * "true". * * @see #toString(Node, boolean) */ public static String toString(final Node xml) { return toString(xml, true); } /** * This is a convenience method that serializes the given XML tree * with indentation; the XML header is included if the second * parameter is "true". * * @see #toString(Node, boolean, boolean) */ public static String toString(final Node xml, final boolean printHeader) { return toString(xml, printHeader, true); } /** * Serializes the given XML tree to string form, including the standard * XML header and indentation if desired. This method relies on the * serialization API from Apache Xerces, since JAXP has on equivalent. * * @param xml * The XML tree to serialize. * @param printHeader * True if you want the XML header printed before the XML. * @param printIndents * True if you want pretty-printing - child elements will be * indented with symmetry. * @return The string representation of the given Node. */ public static String toString(final Node xml, final boolean printHeader, final boolean printIndents) { final short type = xml.getNodeType(); if (type == Node.TEXT_NODE) return xml.getNodeValue(); // // NOTE: This serialization code is not part of JAXP/DOM - it is // specific to Xerces and creates a Xerces dependency for // this class. // final XMLSerializer serializer = new XMLSerializer(); serializer.setNamespaces(true); final OutputFormat formatter = new OutputFormat(); formatter.setOmitXMLDeclaration(!printHeader); formatter.setIndenting(printIndents); serializer.setOutputFormat(formatter); final StringWriter writer = new StringWriter(); serializer.setOutputCharStream(writer); try { if (type == Node.DOCUMENT_NODE) serializer.serialize((Document) xml); else serializer.serialize((Element) xml); } // // we are using a StringWriter, so this "should never happen". the // StringWriter implementation writes to a StringBuffer, so there's // no file I/O that could fail. // // if it DOES fail, we re-throw with a more serious error, because // this a very common operation. // catch (final IOException error) { throw new RuntimeException(error.getMessage(), error); } return writer.toString(); } /** * @param qname * The QName to serialize into prefix:localName form. * @return This method returns a different value than QName.toString(). The * QName.toString() returns a string of the format: <br> * <br> * <em>{namespace URI}:name</em> <br> * <br> * whereas this method returns: <br> * <br> * <em>prefix:name</em> <br> * <br> * which is a valid representation to put into XML documents. If * the QName has no prefix, the local name is returned. */ public static String toString(final QName qname) { final String prefix = qname.getPrefix(); final String name = qname.getLocalPart(); if (prefix == null || prefix.length() == 0) return name; return prefix + ':' + name; } }