Wednesday, February 1, 2012

How to filter particular tag from xml with the help of Regex Pattern in android?

In below example we considering multiple <CONTACT> tag in xml.........which is stored in "getxmlstring" variable

ArrayList<String> contactArray = new ArrayList<String>();


if (getxmlstring.contains("</CONTACT>")) {
        Retrieve_Contact_Tag("<CONTACT></CONTACT>" + getxmlstring);
for (int i = 0; i < contactArray.size(); i++) {
String replacestr = contactArray.get(i).toString();
                                //Now parse the xml according to your Choice
parseContact("<CONTACT>" + replacestr + "</CONTACT>",context);
}
contactArray.clear();
}


private static void Retrieve_Contact_Tag(String xmlContent) {
Pattern regex = Pattern.compile("<CONTACT>(.*?)</CONTACT>",
Pattern.DOTALL);
Matcher matcher = regex.matcher(xmlContent);
// Pattern regex2 = Pattern.compile("<([^<>]+)>([^<>]+)</\\1>");
if (matcher.find()) {
while (matcher.find()) {
contactArray.add((matcher.group(1)));
}
}
}

No comments:

Post a Comment