Source Code : XMLWriter helper class

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

XMLWriter helper class

 
<font color="#3f7f5f">/*
 * 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.
 */</font>


<font color="#7f0055"><b>import</b></font> java.io.IOException;
<font color="#7f0055"><b>import</b></font> java.io.Writer;

<font color="#3f7f5f">/**
 * XMLWriter helper class.
 *
 * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
 */</font>
<font color="#7f0055"><b>public</b></font> <font color="#7f0055"><b>class</b></font> XMLWriter {


    <font color="#3f7f5f">// -------------------------------------------------------------- Constants
</font>

    <font color="#3f7f5f">/**
     * Opening tag.
     */</font>
    <font color="#7f0055"><b>public</b></font> <font color="#7f0055"><b>static</b></font> <font color="#7f0055"><b>final</b></font> <font color="#7f0055"><b>int</b></font> OPENING = 0;


    <font color="#3f7f5f">/**
     * Closing tag.
     */</font>
    <font color="#7f0055"><b>public</b></font> <font color="#7f0055"><b>static</b></font> <font color="#7f0055"><b>final</b></font> <font color="#7f0055"><b>int</b></font> CLOSING = 1;


    <font color="#3f7f5f">/**
     * Element with no content.
     */</font>
    <font color="#7f0055"><b>public</b></font> <font color="#7f0055"><b>static</b></font> <font color="#7f0055"><b>final</b></font> <font color="#7f0055"><b>int</b></font> NO_CONTENT = 2;


    <font color="#3f7f5f">// ----------------------------------------------------- Instance Variables
</font>

    <font color="#3f7f5f">/**
     * Buffer.
     */</font>
    <font color="#7f0055"><b>protected</b></font> StringBuffer buffer = <font color="#7f0055"><b>new</b></font> StringBuffer();


    <font color="#3f7f5f">/**
     * Writer.
     */</font>
    <font color="#7f0055"><b>protected</b></font> Writer writer = null;


    <font color="#3f7f5f">// ----------------------------------------------------------- Constructors
</font>

    <font color="#3f7f5f">/**
     * Constructor.
     */</font>
    <font color="#7f0055"><b>public</b></font> XMLWriter() {
    }


    <font color="#3f7f5f">/**
     * Constructor.
     */</font>
    <font color="#7f0055"><b>public</b></font> XMLWriter(Writer writer) {
        this.writer = writer;
    }


    <font color="#3f7f5f">// --------------------------------------------------------- Public Methods
</font>

    <font color="#3f7f5f">/**
     * Retrieve generated XML.
     *
     * @return String containing the generated XML
     */</font>
    <font color="#7f0055"><b>public</b></font> String toString() {
        <font color="#7f0055"><b>return</b></font> buffer.toString();
    }


    <font color="#3f7f5f">/**
     * Write property to the XML.
     *
     * @param namespace Namespace
     * @param namespaceInfo Namespace info
     * @param name Property name
     * @param value Property value
     */</font>
    <font color="#7f0055"><b>public</b></font> <font color="#7f0055"><b>void</b></font> writeProperty(String namespace, String namespaceInfo,
                              String name, String value) {
        writeElement(namespace, namespaceInfo, name, OPENING);
        buffer.append(value);
        writeElement(namespace, namespaceInfo, name, CLOSING);

    }


    <font color="#3f7f5f">/**
     * Write property to the XML.
     *
     * @param namespace Namespace
     * @param name Property name
     * @param value Property value
     */</font>
    <font color="#7f0055"><b>public</b></font> <font color="#7f0055"><b>void</b></font> writeProperty(String namespace, String name, String value) {
        writeElement(namespace, name, OPENING);
        buffer.append(value);
        writeElement(namespace, name, CLOSING);
    }


    <font color="#3f7f5f">/**
     * Write property to the XML.
     *
     * @param namespace Namespace
     * @param name Property name
     */</font>
    <font color="#7f0055"><b>public</b></font> <font color="#7f0055"><b>void</b></font> writeProperty(String namespace, String name) {
        writeElement(namespace, name, NO_CONTENT);
    }


    <font color="#3f7f5f">/**
     * Write an element.
     *
     * @param name Element name
     * @param namespace Namespace abbreviation
     * @param type Element type
     */</font>
    <font color="#7f0055"><b>public</b></font> <font color="#7f0055"><b>void</b></font> writeElement(String namespace, String name, <font color="#7f0055"><b>int</b></font> type) {
        writeElement(namespace, null, name, type);
    }


    <font color="#3f7f5f">/**
     * Write an element.
     *
     * @param namespace Namespace abbreviation
     * @param namespaceInfo Namespace info
     * @param name Element name
     * @param type Element type
     */</font>
    <font color="#7f0055"><b>public</b></font> <font color="#7f0055"><b>void</b></font> writeElement(String namespace, String namespaceInfo,
                             String name, <font color="#7f0055"><b>int</b></font> type) {
        <font color="#7f0055"><b>if</b></font> ((namespace != null) && (namespace.length() > 0)) {
            <font color="#7f0055"><b>switch</b></font> (type) {
            <font color="#7f0055"><b>case</b></font> OPENING:
                <font color="#7f0055"><b>if</b></font> (namespaceInfo != null) {
                    buffer.append(<font color="#2a00ff">"<"</font> + namespace + <font color="#2a00ff">":"</font> + name + <font color="#2a00ff">" xmlns:"</font>
                                  + namespace + <font color="#2a00ff">"=""</font>
                                  + namespaceInfo + <font color="#2a00ff">"">"</font>);
                } <font color="#7f0055"><b>else</b></font> {
                    buffer.append(<font color="#2a00ff">"<"</font> + namespace + <font color="#2a00ff">":"</font> + name + <font color="#2a00ff">">"</font>);
                }
                <font color="#7f0055"><b>break</b></font>;
            <font color="#7f0055"><b>case</b></font> CLOSING:
                buffer.append(<font color="#2a00ff">"</"</font> + namespace + <font color="#2a00ff">":"</font> + name + <font color="#2a00ff">">
"</font>);
                <font color="#7f0055"><b>break</b></font>;
            <font color="#7f0055"><b>case</b></font> NO_CONTENT:
            <font color="#7f0055"><b>default</b></font>:
                <font color="#7f0055"><b>if</b></font> (namespaceInfo != null) {
                    buffer.append(<font color="#2a00ff">"<"</font> + namespace + <font color="#2a00ff">":"</font> + name + <font color="#2a00ff">" xmlns:"</font>
                                  + namespace + <font color="#2a00ff">"=""</font>
                                  + namespaceInfo + <font color="#2a00ff">""/>"</font>);
                } <font color="#7f0055"><b>else</b></font> {
                    buffer.append(<font color="#2a00ff">"<"</font> + namespace + <font color="#2a00ff">":"</font> + name + <font color="#2a00ff">"/>"</font>);
                }
                <font color="#7f0055"><b>break</b></font>;
            }
        } <font color="#7f0055"><b>else</b></font> {
            <font color="#7f0055"><b>switch</b></font> (type) {
            <font color="#7f0055"><b>case</b></font> OPENING:
                buffer.append(<font color="#2a00ff">"<"</font> + name + <font color="#2a00ff">">"</font>);
                <font color="#7f0055"><b>break</b></font>;
            <font color="#7f0055"><b>case</b></font> CLOSING:
                buffer.append(<font color="#2a00ff">"</"</font> + name + <font color="#2a00ff">">
"</font>);
                <font color="#7f0055"><b>break</b></font>;
            <font color="#7f0055"><b>case</b></font> NO_CONTENT:
            <font color="#7f0055"><b>default</b></font>:
                buffer.append(<font color="#2a00ff">"<"</font> + name + <font color="#2a00ff">"/>"</font>);
                <font color="#7f0055"><b>break</b></font>;
            }
        }
    }


    <font color="#3f7f5f">/**
     * Write text.
     *
     * @param text Text to append
     */</font>
    <font color="#7f0055"><b>public</b></font> <font color="#7f0055"><b>void</b></font> writeText(String text) {
        buffer.append(text);
    }


    <font color="#3f7f5f">/**
     * Write data.
     *
     * @param data Data to append
     */</font>
    <font color="#7f0055"><b>public</b></font> <font color="#7f0055"><b>void</b></font> writeData(String data) {
        buffer.append(data);
    }


    <font color="#3f7f5f">/**
     * Write XML Header.
     */</font>
    <font color="#7f0055"><b>public</b></font> <font color="#7f0055"><b>void</b></font> writeXMLHeader() {
        buffer.append(<font color="#2a00ff">"<?xml version="1.0" encoding="utf-8" ?>
"</font>);
    }


    <font color="#3f7f5f">/**
     * Send data and reinitializes buffer.
     */</font>
    <font color="#7f0055"><b>public</b></font> <font color="#7f0055"><b>void</b></font> sendData()
        <font color="#7f0055"><b>throws</b></font> IOException {
        <font color="#7f0055"><b>if</b></font> (writer != null) {
            writer.write(buffer.toString());
            buffer = <font color="#7f0055"><b>new</b></font> StringBuffer();
        }
    }


}

   
  

Thank with us

Sponsored Adds
Mechanical Animals!! W0W guaranteed
WIN! Cheap!! Unlimited phone calls ! forever! WOW!