title: Parsing XML with REGEX in Java
body: <p>Given the below XML snippet I need to get a list of name/value pairs for each child under DataElements. XPath or an XML parser cannot be used for reasons beyond my control so I am using regex.</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;StandardDataObject xmlns="myns"&gt;
    &lt;DataElements&gt;
      &lt;EmpStatus&gt;2.0&lt;/EmpStatus&gt;
      &lt;Expenditure&gt;95465.00&lt;/Expenditure&gt;
      &lt;StaffType&gt;11.A&lt;/StaffType&gt;
      &lt;Industry&gt;13&lt;/Industry&gt;
    &lt;/DataElements&gt;
    &lt;InteractionElements&gt;
      &lt;TargetCenter&gt;92f4-MPA&lt;/TargetCenter&gt;
      &lt;Trace&gt;7.19879&lt;/Trace&gt;
    &lt;/InteractionElements&gt;
  &lt;/StandardDataObject&gt;
  </code></pre>
  
  <p>The output I need is: 
      [{EmpStatus:2.0},  {Expenditure:95465.00}, {StaffType:11.A}, {Industry:13}]  </p>
  
  <p>The tag names under DataElements are dynamic and so cannot be expressed literally in the regex. The tag names TargetCenter and Trace are static and could be in the regex but if there is a way to avoid hardcoding that would be preferable. </p>
  
  <pre><code>"&lt;([A-Za-z0-9]+?)&gt;([A-Za-z0-9.]*?)&lt;/"
  </code></pre>
  
  <p>This is the regex I have constructed and it has the problem that it erroneously includes {Trace:719879} in the results.  Relying on new-lines within the XML or any other apparent formatting is not an option.</p>
  
  <p>Below is an approximation of the Java code I am using:</p>
  
  <pre><code>private static final Pattern PATTERN_1 = Pattern.compile(..REGEX..);
  private List&lt;DataElement&gt; listDataElements(CharSequence cs) {
      List&lt;DataElement&gt; list = new ArrayList&lt;DataElement&gt;();
      Matcher matcher = PATTERN_1.matcher(cs);
      while (matcher.find()) {
          list.add(new DataElement(matcher.group(1), matcher.group(2)));
      }
      return list;
  }
  </code></pre>
  
  <p>How can I change my regex to only include data elements and ignore the rest?</p>