title: Flattening any xml record in java
body: <p>So basically, I am having problem turning my head around this one.</p>
  
  <p>Basically, I can have any type of xml.. and I have to parse it.</p>
  
  <p>I am able to parse it succesfully for "flat" xml.
  So for example:</p>
  
  <pre><code>&lt;emp&gt;
  &lt;id&gt;1&lt;/id&gt;
  &lt;name&gt;foo&lt;/name&gt;
  &lt;age&gt;22&lt;/age&gt;
  &lt;/emp&gt;
  </code></pre>
  
  <p>My simple parser works fine for this (note that schema is variable.. any flat xml (no hardcoding)..</p>
  
  <p>But it fails for nested xml stuff
  So</p>
  
  <pre><code>&lt;emplist&gt;
  &lt;emp&gt;
     &lt;manager&gt;
     &lt;id&gt;1&lt;/id&gt;
     &lt;name&gt;foo&lt;/name&gt;
     &lt;/manager&gt;
  &lt;/emp&gt;
  &lt;emp&gt;
     &lt;clerk&gt;
     &lt;cid&gt;1&lt;/cid&gt;
     &lt;cname&gt;foo&lt;/cname&gt;
     &lt;/clerk&gt;
  &lt;/emp&gt;
   &lt;/emplist&gt;
  </code></pre>
  
  <p>The output I want for first case (which i am getting is )</p>
  
  <pre><code>id,1
  name,foo
  </code></pre>
  
  <p>but for second I want</p>
  
  <pre><code>id, 1
  name, foo
  cid, 1
  cname,foo
  </code></pre>
  
  <p>How do i flatten this out.
  Thanks</p>
  
  <p>Current code:</p>
  
  <pre><code>public class XMLReader {
      public static void main(String[] args) throws JDOMException, IOException {
  
          //String xmlString = "&lt;employee &gt;\n &lt;firstname xml:space=\"preserve\" &gt;John&lt;/firstname&gt;\n &lt;lastname&gt;Watson&lt;/lastname&gt;\n &lt;age&gt;30&lt;/age&gt;\n &lt;email&gt;johnwatson@sh.com&lt;/email&gt;\n&lt;/employee&gt;";
          String xmlString = "&lt;employee&gt;\n" + 
                  "       &lt;personal&gt;&lt;id&gt;2D61EC47-0F56-5A33-6057-54DB0ABBDBF0&lt;/id&gt;\n" + 
                  "       &lt;name&gt;Lareina&lt;/name&gt;\n" + 
                  "       &lt;age&gt;50&lt;/age&gt;\n" + 
                  "       &lt;/personal&gt;&lt;contact&gt;&lt;dept&gt;Fusce&lt;/dept&gt;\n" + 
                  "       &lt;manager&gt;B55E6DA8-76BD-A3C8-2DDF-686CB9A0BB76&lt;/manager&gt;&lt;/contact&gt;\n" + 
                  "   &lt;/employee&gt;";
          System.out.println(xmlString);
  
  
          SAXBuilder builder = new SAXBuilder();
          Reader in = new StringReader(xmlString);
  
          Document doc = builder.build(in);
          Element root = doc.getRootElement();
          List children = root.getChildren();
          //System.out.println(children);
          String value = "";
          for (int i = 0; i &lt; children.size(); i++) {
  
                  Element dataNode = (Element) children.get(i);
                 // Element dataNode = (Element) dataNodes.get(j);
                  value += ", " +dataNode.getText().trim();
                  System.out.println(dataNode.getName() + " : " + dataNode.getText());
  
                  //context.write(new Text(rowKey.toString()), new Text(node.getName().trim() + " " + node.getText().trim()));
  
              }
          //System.out.println(in);
  
  
  
      }
  }
  </code></pre>