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.

HpTerm
  • 8,151
  • 12
  • 51
  • 67
Ashok.N
  • 1,327
  • 9
  • 29
  • 64
  • 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. – Wim Ombelets Apr 14 '14 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. – Ashok.N Apr 14 '14 at 12:05
  • true, but it looks like a valid xml fragment, perhaps http://stackoverflow.com/questions/729621/convert-string-xml-fragment-to-document-node-in-java can give you some ideas. – Wim Ombelets Apr 14 '14 at 12:15

1 Answers1

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.

Kent
  • 189,393
  • 32
  • 233
  • 301