简体   繁体   中英

Extract string between xml tags in android without parsing the xml

I have a very simple XML like this:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://icacec.com/">TRUE,Hrithik Sharma,201301-11</string>

Now, I want to extract only TRUE , Hrithik Sharma , 201301-11 in 3 separate variables .

I could split the string based on the "," like this:

String[] parts = responseBody.split(",");
String response_auth = parts[0];
String user_name = parts[1];    
String user_number=parts[2];

But the problem which I am facing is that, the Strings are not getting extracted independently. To be more precise, without the XML tags. How should I achieve that?

This could solve this simple case, but without parsing what are you going to do with other conditions?

public static void main(String[] args) {
    String raw = "<string xmlns=\"http://icacec.com/\">TRUE,Hrithik Sharma,201301-11</string>";
    raw = raw.substring(0, raw.lastIndexOf("<"));
    raw = raw.substring(raw.lastIndexOf(">") + 1, raw.length());
    String [] contents = raw.split(",");
    for (String txt : contents)
        System.out.println(txt);
}

This is highly discouraged unless you actually know what you are getting in XML

responseBody:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://icacec.com/">TRUE,Hrithik Sharma,201301-11</string>

code:

String[] parts = responseBody.split(">");
String tagsFirst= parts[0];
String usefull = parts[2];    

String[] actualBody = usefull.split("<");

String content = actualBody[0];
String[] contentParts=content.split(",");
//now you can have the three parts:
String truefalse=contentParts[0];
String name=contentParts[1];
String date=contentParts[2];

Try this regex -

"<string xmlns=\"http://icacec.com/\">(.+),(.+),(.+)</string>"

Capture groups 1, 2, and 3 will contain your three items, ie:

Pattern pattern = Pattern.compile("<string xmlns=\"http://icacec.com/\">(.+),(.+),(.+)</string>");
Matcher matcher = pattern.matcher("<string xmlns=\"http://icacec.com/\">TRUE,Hrithik Sharma,201301-11</string>");

if(matcher.matches())
{
    System.out.println("Bool: " + matcher.group(1));
    System.out.println("Name: " + matcher.group(2));
    System.out.println("Date: " + matcher.group(3));
}

Try to split like:

String[] strTemp = strXMLOutput.split("<TagName>");
strTemp = strTemp[1].split("</TagName>");
String strValue = strTemp[0]

100% it'll work.

You can try this:

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Test {

    public static void main(String[] args) {
        String responseBody = null;
        String inputString = "<?xml version=\"1.0\" encoding=\"utf-8\"?><string xmlns=\"http://icacec.com/\">TRUE,Hrithik Sharma,201301-11</string>";
        String regex = "<string[^>]*>(.+?)</string\\s*>";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(inputString);
        while(matcher.find()) {
            responseBody = matcher.group(1);
            System.out.println(responseBody);
        }

        String[] splits = responseBody.split(",");
        System.out.println(splits[0]);/*prints TRUE*/
        System.out.println(splits[1]);/*prints Hrithik Sharma*/
        System.out.println(splits[2]);/*201301-11*/

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM