Java tutorial
//package com.java2s; /* * Created on 16/07/2004 * YAWLEditor v1.01 * * @author Lindsay Bradford * * * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { /** * Simple regular-expression based method to strip the outermost tags from a fragment of XML. * Assumes that the fragment begins and ends with tags. * @param xmlFragment * @return the xmlFragment string with the outermost tags removed. */ public static String stripOutermostTags(String xmlFragment) { if (xmlFragment == null) { return null; } Pattern tagContainingPattern = Pattern.compile("^<.*?>(.*)</.*?>$"); if (tagContainingPattern == null) { return xmlFragment; } Matcher tagContainingMatcher = tagContainingPattern.matcher(xmlFragment); if (tagContainingMatcher == null) { return xmlFragment; } if (tagContainingMatcher.find()) { tagContainingMatcher.group(); return tagContainingMatcher.replaceAll("$1"); } return xmlFragment; // *shrug* no tags in the fragment, apparently. } }