title: regex to match tags data from multiple similar tags
body: <p>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.<br>
  Regex:</p>
  
  <pre><code>public static String selectCxmlFromXml(String xml) {
      String cxml = "";
      Pattern pattern = Pattern.compile(".*(&lt;cXML.*&lt;/cXML&gt;).*", Pattern.DOTALL);
      Matcher matcher = pattern.matcher(xml);
      if (matcher.matches()) {
          cxml = matcher.group(1);
      }
      return cxml;
  }
  </code></pre>
  
  <p>The input XML(which is not a valid XML):</p>
  
  <pre><code>&lt;!DOCTYPE cXML SYSTEM "http://xml.cxml.org/schemas/cXML/1.2.037/cXML.dtd"&gt;1532298890669-3937185683464996079@216.109.111.68
  &lt;cXML payloadID="1532333614215-4720337288049634328@216.109.111.11" timestamp="2018-07-23T01:13:34-07:00"&gt;
     &lt;Response&gt;
        &lt;Status code="200" text="OK"/&gt;
        &lt;GetPendingResponse&gt;
           &lt;cXML
              payloadID="1532333614208-7005041222787302474@216.109.111.11" timestamp="2018-07-23T01:13:34-07:00"&gt;
              &lt;Message&gt;
                 &lt;DataAvailableMessage&gt;
                    &lt;InternalID domain="PendingMessages"&gt;140000000000000000010977255&lt;/InternalID&gt;
                 &lt;/DataAvailableMessage&gt;
              &lt;/Message&gt;
           &lt;/cXML&gt;
        &lt;/GetPendingResponse&gt;
     &lt;/Response&gt;
  &lt;/cXML&gt;
  </code></pre>
  
  <p>expected response:</p>
  
  <pre><code>&lt;cXML payloadID="1532333614215-4720337288049634328@216.109.111.11" timestamp="2018-07-23T01:13:34-07:00"&gt;
     &lt;Response&gt;
        &lt;Status code="200" text="OK"/&gt;
        &lt;GetPendingResponse&gt;
           &lt;cXML
              payloadID="1532333614208-7005041222787302474@216.109.111.11" timestamp="2018-07-23T01:13:34-07:00"&gt;
              &lt;Message&gt;
                 &lt;DataAvailableMessage&gt;
                    &lt;InternalID domain="PendingMessages"&gt;140000000000000000010977255&lt;/InternalID&gt;
                 &lt;/DataAvailableMessage&gt;
              &lt;/Message&gt;
           &lt;/cXML&gt;
        &lt;/GetPendingResponse&gt;
     &lt;/Response&gt;
  &lt;/cXML&gt;
  </code></pre>
  
  <p>response got(incorrect):</p>
  
  <pre><code>&lt;cXML payloadID="1532333614208-7005041222787302474@216.109.111.11" timestamp="2018-07-23T01:13:34-07:00"&gt;
              &lt;Message&gt;
                 &lt;DataAvailableMessage&gt;
                    &lt;InternalID domain="PendingMessages"&gt;140000000000000000010977255&lt;/InternalID&gt;
                 &lt;/DataAvailableMessage&gt;
              &lt;/Message&gt;
           &lt;/cXML&gt;
        &lt;/GetPendingResponse&gt;
     &lt;/Response&gt;
  &lt;/cXML&gt;
  </code></pre>
  
  <p>notice that the first cxml declaration and its data got skipped.<br>
  If I use <code>.*(&lt;cXML.*?&lt;/cXML&gt;).*</code>, then only the internal cxml got selected and the external cxml get skipped.</p>
  
  <pre><code>&lt;cXML payloadID="1532333614208-7005041222787302474@216.109.111.11" timestamp="2018-07-23T01:13:34-07:00"&gt;
     &lt;Message&gt;
          &lt;DataAvailableMessage&gt;
             &lt;InternalID domain="PendingMessages"&gt;140000000000000000010977255&lt;/InternalID&gt;
          &lt;/DataAvailableMessage&gt;
       &lt;/Message&gt;
  &lt;/cXML&gt;  
  </code></pre>
  
  <p>What am I doing wrong here? </p>