add New Lines To XML Fields - Android XML

Android examples for XML:XML String

Description

add New Lines To XML Fields

Demo Code

/**//from w ww .  j a v a  2s. co m
 * Created by IntelliJ IDEA.
 * User: Piek Vossen
 * Date: 17-dec-2008
 * Time: 14:38:37
 * To change this template use File | Settings | File Templates.
 * This file is part of KafSaxParser.

 KafSaxParser is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 KafSaxParser is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with KafSaxParser.  If not, see <http://www.gnu.org/licenses/>.
 */
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String value = "java2s.com";
        System.out.println(addNewLinesToFields2(value));
    }

    public static String addNewLinesToFields2(String value) {
        String newValue = "";
        int idx_s = 0;
        int idx_ts = value.indexOf("</");
        int idx_te = -1;
        if (idx_ts > -1) {
            idx_te = value.indexOf(">", idx_ts);
        }

        while (idx_te != -1) {
            newValue += value.substring(idx_s, idx_te + 1);
            newValue += "\n";
            //          System.out.println(newValue);
            idx_s = idx_te + 1;
            if (idx_s < value.length()) {
                idx_ts = value.indexOf("</", idx_s);
                if (idx_ts > -1) {
                    idx_te = value.indexOf(">", idx_ts);
                } else {
                    idx_te = -1;
                }
            } else {
                break;
            }
        }
        if (idx_s < value.length()) {
            newValue += value.substring(idx_s);
            newValue += "\n";
        }
        return newValue;
    }
}

Related Tutorials