Here you can find the source of getValueInsideXmlTags(String xml, String tag)
Parameter | Description |
---|---|
xml | a parameter |
tag | - without brackets, tag="html" will mean give me everything between <html> and </html>. |
public static String getValueInsideXmlTags(String xml, String tag)
//package com.java2s; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { /**/*ww w . j ava 2 s .c o m*/ * * @param xml * @param openTag - for example openTag="<a>" * @param closeTag - for example closeTag="</a>" * @return */ public static String getValueInsideXmlTags(String xml, String openTag, String closeTag) { String regexCode = "(?<=" + openTag + ")(.*?)(?=" + closeTag + ")"; return regexExtract(xml, regexCode); } /** * @param xml * @param tag - without brackets, tag="html" will mean give me everything between <html> and * </html>. * @return */ public static String getValueInsideXmlTags(String xml, String tag) { return getValueInsideXmlTags(xml, "<" + tag + ">", "</" + tag + ">"); } /** * Extracts regex match from string * @param string to search * @param pattern regex pattern * @return matched string, or null if nothing found (or null if any of the parameters is null) */ public static String regexExtract(String string, String pattern) { if (string == null || pattern == null) { return null; } Matcher m = regexGetMatcherForPattern(string, pattern); if (m == null) { return null; } if (m.find()) { return m.group(0); } else return null; } public static Matcher regexGetMatcherForPattern(String string, String pattern) { if (string == null || pattern == null) { return null; } /** * strip newline characters because it doesn't work well with this. */ // string = string.replace("\n", ""); Pattern p = Pattern.compile(pattern, Pattern.DOTALL | Pattern.MULTILINE); Matcher m = p.matcher(string); return m; } }