Q:

regex to match tags data from multiple similar tags

I am trying to extract the cxml from the below mentioned XML(can be valid or invalid) which contain multiple cxml tags. I want top most cxml along with its child cxml tags but result I am getting is not as expected.
Regex:
public static String selectCxmlFromXml(String xml) {
    String cxml = "";
    Pattern pattern = Pattern.compile(".*(<cXML.*</cXML>).*", Pattern.DOTALL);
    Matcher matcher = pattern.matcher(xml);
    if (matcher.matches()) {
        cxml = matcher.group(1);
    }
    return cxml;
}

The input XML(which is not a valid XML):
<!DOCTYPE cXML SYSTEM "http://xml.cxml.org/schemas/cXML/1.2.037/cXML.dtd">1532298890669-3937185683464996079@216.109.111.68
<cXML payloadID="1532333614215-4720337288049634328@216.109.111.11" timestamp="2018-07-23T01:13:34-07:00">
   <Response>
      <Status code="200" text="OK"/>
      <GetPendingResponse>
         <cXML
            payloadID="1532333614208-7005041222787302474@216.109.111.11" timestamp="2018-07-23T01:13:34-07:00">
            <Message>
               <DataAvailableMessage>
                  <InternalID domain="PendingMessages">140000000000000000010977255</InternalID>
               </DataAvailableMessage>
            </Message>
         </cXML>
      </GetPendingResponse>
   </Response>
</cXML>

expected response:
<cXML payloadID="1532333614215-4720337288049634328@216.109.111.11" timestamp="2018-07-23T01:13:34-07:00">
   <Response>
      <Status code="200" text="OK"/>
      <GetPendingResponse>
         <cXML
            payloadID="1532333614208-7005041222787302474@216.109.111.11" timestamp="2018-07-23T01:13:34-07:00">
            <Message>
               <DataAvailableMessage>
                  <InternalID domain="PendingMessages">140000000000000000010977255</InternalID>
               </DataAvailableMessage>
            </Message>
         </cXML>
      </GetPendingResponse>
   </Response>
</cXML>

response got(incorrect):
<cXML payloadID="1532333614208-7005041222787302474@216.109.111.11" timestamp="2018-07-23T01:13:34-07:00">
            <Message>
               <DataAvailableMessage>
                  <InternalID domain="PendingMessages">140000000000000000010977255</InternalID>
               </DataAvailableMessage>
            </Message>
         </cXML>
      </GetPendingResponse>
   </Response>
</cXML>

notice that the first cxml declaration and its data got skipped.
If I use .*(<cXML.*?</cXML>).*, then only the internal cxml got selected and the external cxml get skipped.
<cXML payloadID="1532333614208-7005041222787302474@216.109.111.11" timestamp="2018-07-23T01:13:34-07:00">
   <Message>
        <DataAvailableMessage>
           <InternalID domain="PendingMessages">140000000000000000010977255</InternalID>
        </DataAvailableMessage>
     </Message>
</cXML>  

What am I doing wrong here? 

A:

I found out what I was doing wrong. two things:
1. Use find() instead of match()
2. change regex to (<cXML.*</cXML>) which will capture the entire cXML
New method would be:
public static String selectCxmlFromXml(String xml) {
    String cxml = "";
    Pattern pattern = Pattern.compile("(<cXML.*</cXML>)", Pattern.DOTALL);
    Matcher matcher = pattern.matcher(xml);
    if (matcher.find()) {
        cxml = matcher.group(1);
    }
    return cxml;
}

