1

I have the following string. I want to extract the string between two words <cases> and </cases> using Java. I want to extract the string to form a valid xml as per my requirement.

<cases><Final-Results><row>
          <CRDATTIM>2014-03-26-05.22.22.339840</CRDATTIM>
          <RECORDCD>C</RECORDCD>
          <CRNODE>01</CRNODE>
          <CKEY>2014-03-26-05.22.22.339840C01</CKEY>
          <STATCD>CREATED</STATCD>
          <UNITCD>CSMHCQA</UNITCD>
          <WRKTYPE>CALL</WRKTYPE>
          <issues><row>
      <IKEY>2014-03-26-05.22.22.193840T01</IKEY>
      <PRTY>999</PRTY>
      <ISSUEID>20140326-155047-DT81694</ISSUEID>
      <SUBJECT>Group</SUBJECT>
      <ISSTYP>GROUP</ISSTYP>
      <ISSCAT1>GROUP INQUIRY</ISSCAT1>
     </row></issues></row></Final-Results></cases><?xml version="1.0" encoding="UTF-8"?>
     <response>
        <FOLDERID>COMM*H</FOLDERID>
     </response>

I have tried with the following code. But its not working for me.

Pattern pattern = Pattern.compile("<cases>(.*?)</cases>");
Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
    System.out.println(matcher.group(1));
}

Am I doing any mistake here?Could please share your idea to solve my issue? Thanks in advance.

3
  • 2
    please don't parse xml using pattern matching. There are, in almost every imaginable language, constructs to deal with xml. Use those. It will make your life a lot easier. Commented Apr 14, 2014 at 12:02
  • I agree. But my xml string is not a well formed one. So I am thinking of extracting the content using Pattern matching. Commented Apr 14, 2014 at 12:05
  • true, but it looks like a valid xml fragment, perhaps stackoverflow.com/questions/729621/… can give you some ideas. Commented Apr 14, 2014 at 12:15

1 Answer 1

4

try changing:

Pattern pattern = Pattern.compile("<cases>(.*?)</cases>");

into

Pattern pattern = Pattern.compile("<cases>(.*?)</cases>", Pattern.DOTALL);

btw, if it was a well-formed xml document, use parser instead of regex to handle it.

Sign up to request clarification or add additional context in comments.

1 Comment

Worked perfectly! Thanks Kent!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.