Strips all XML tags (converts to plain text). - Java XML

Java examples for XML:XML String

Description

Strips all XML tags (converts to plain text).

Demo Code

/* *************************************************************************
 *
 *  TMPotter - Bi-text Aligner/TMX Editor
 *
 *  Copyright (C) 2015 Hiroshi Miura//from w  w  w.  j  av a2  s  . c  om
 *
 *  Part of this come from OmegaT.
 *
 *  Copyright (C) 2000-2006 Keith Godfrey and Maxym Mykhalchuk
 *
 *  This file is part of TMPotter.
 *
 *  TMPotter 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.
 *
 *  TMPotter 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 TMPotter.  If not, see http://www.gnu.org/licenses/.
 *
 * *************************************************************************/
//package com.java2s;

import java.util.regex.Pattern;

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

    private static final String RE_TAG = "<\\/?[a-zA-Z]+[0-9]+\\/?>";

    /**
     * Strips all XML tags (converts to plain text). Tags detected only by
     * pattern. Protected parts are not used.
     */
    public static String stripXmlTags(String xml) {
        return Pattern.compile(RE_TAG).matcher(xml).replaceAll("");
    }
}

Related Tutorials