title: parse XML and convert to a Collection
body: <pre><code>&lt;inputs&gt;
    &lt;MAT_NO&gt;123&lt;/MAT_NO&gt;
    &lt;MAT_NO&gt;323&lt;/MAT_NO&gt;
    &lt;MAT_NO&gt;4223&lt;/MAT_NO&gt;
    &lt;FOO_BAR&gt;122&lt;/FOO_BAR&gt;
    &lt;FOO_BAR&gt;125&lt;/FOO_BAR&gt;
  &lt;/inputs&gt;
  </code></pre>
  
  <p>I've to parse the above the XML. After parsing, i want the values to be in a <code>Map&lt;String, List&lt;String&gt;&gt;</code> with Key values corresponding to the child nodes - MAT_NO, FOO_BAR
  and values - the values of the child nodes -123, 323 etc.</p>
  
  <p>Following is my shot. Is there any better way of doing this??</p>
  
  <pre><code>public class UserInputsXmlParser extends DefaultHandler {
  
   private final SaveSubscriptionValues subscriptionValues = null;
   private String nodeValue = "";
   private final String inputKey = "";
   private final List&lt;String&gt; valuesList = null;
   private Map&lt;String, List&lt;String&gt;&gt; userInputs;
  
   public Map&lt;String, List&lt;String&gt;&gt; parse(final String strXML) {
    try {
     final SAXParserFactory parserFactory = SAXParserFactory
       .newInstance();
     final SAXParser saxParser = parserFactory.newSAXParser();
     saxParser.parse(new InputSource(new StringReader(strXML)), this);
     return userInputs;
    } catch (final SAXException e) {
     e.printStackTrace();
     throw new MyException("", e);
    } catch (final IOException e) {
     e.printStackTrace();
     throw new MyException("", e);
    } catch (final ParserConfigurationException e) {
     e.printStackTrace();
     throw new MyException("", e);
    } catch (final Exception e) {
     e.printStackTrace();
     throw new MyException("", e);
    }
   }
  
   @Override
   public void startElement(final String uri, final String localName,
     final String qName, final Attributes attributes)
     throws SAXException {
    nodeValue = "";
  
    if ("inputs".equalsIgnoreCase(qName)) {
     userInputs = MyUtil.getNewHashMap();
     return;
    }
  
   }
  
   @Override
   public void characters(final char[] ch, final int start, final int length)
     throws SAXException {
    if (!MyUtil.isEmpty(nodeValue)) {
     nodeValue += new String(ch, start, length);
    } else {
     nodeValue = new String(ch, start, length);
    }
   }
  
   @Override
   public void endElement(final String uri, final String localName,
     final String qName) throws SAXException {
    if (!"inputs".equalsIgnoreCase(qName)) {
     storeUserInputs(qName, nodeValue);
    }
  
   }
  
   /**
    * @param qName
    * @param nodeValue2
    */
   private void storeUserInputs(final String qName, final String nodeValue2) {
    if (nodeValue2 == null || nodeValue2.trim().equals("")) { return; }
    final String trimmedValue = nodeValue2.trim();
    final List&lt;String&gt; values = userInputs.get(qName);
    if (values != null) {
     values.add(trimmedValue);
    } else {
     final List&lt;String&gt; valueList = new ArrayList&lt;String&gt;();
     valueList.add(trimmedValue);
     userInputs.put(qName, valueList);
    }
   }
  
   public static void main(final String[] args) {
    final String sample = "&lt;inputs&gt;" + "&lt;MAT_NO&gt;154400-0000&lt;/MAT_NO&gt;"
      + "&lt;MAT_NO&gt;  &lt;/MAT_NO&gt;" + "&lt;MAT_NO&gt;154400-0002&lt;/MAT_NO&gt;"
      + "&lt;PAT_NO&gt;123&lt;/PAT_NO&gt;&lt;PAT_NO&gt;1111&lt;/PAT_NO&gt;&lt;/inputs&gt;";
    System.out.println(new UserInputsXmlParser().parse(sample));
   }
  
  }
  </code></pre>
  
  <p><strong>UPDATE:</strong> The children of <code>&lt;inputs&gt;</code> nodes are dynamic. I'll be knowing just the root node.</p>