title: parse xml nested nodes that repeat with SAX
body: <p>I have looked for days and I can't seem to wrap my head around my issue. I have been able to parse the xml doc but it has child nodes that repeat before moving back up to the parent node. My parser seems to be iterating through the child nodes correctly but I can only see the results of the last child node which is repeated multiple times. </p>
  
  <p>If a parent node contains 5 child nodes, it prints the result of the last of the 5 nodes 5 times.</p>
  
  <p>I need the xml tags after "portfolio" and "trade" to parse correctly. Ultimately get the xml tags before the  tag to line up and print with the child nodes after "trade" without repeating the last node inside "trade"</p>
  
  <p>Examples are appreciated
  Thank you </p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.List;
  import org.xml.sax.Attributes;
  import org.xml.sax.SAXException;
  import org.xml.sax.helpers.DefaultHandler;
  
  
  
  
  public class MyHandler extends DefaultHandler {
  //List to hold trade object
  private List&lt;Portfolio&gt; tradeList = null;
  private Portfolio trd = null;
  
  //getter method for trade list
  public List&lt;Portfolio&gt; getEmpList() {
      return tradeList;
  }
  
  
  boolean bdate = false;
  boolean bfirm = false;
  boolean bacctId = false;
  boolean bUserId = false;
  boolean bseg = false;
  boolean btradedate = false;
  boolean btradetime = false;
  boolean bec = false;
  boolean bexch = false;
  boolean bpfcode = false;
  boolean bpftype = false;
  boolean bpe = false;
  boolean btradeqty = false;
  boolean btradeprice = false;
  boolean bmore = false;
  
  @Override
  public void startElement(String uri, String localName, String qName, Attributes     attributes)
          throws SAXException {
  
      if (qName.equalsIgnoreCase("portfolio")) {
          trd = new Portfolio();
          //initialize list
          if (tradeList == null)
              tradeList = new ArrayList&lt;&gt;();  
      } else if (qName.equalsIgnoreCase("firm")) {
          bfirm = true;
      } else if (qName.equalsIgnoreCase("acctId")) {
          bacctId = true;
      } else if (qName.equalsIgnoreCase("UserId")) {
          bUserId = true;
      } else if (qName.equalsIgnoreCase("seg")) {
          bseg = true;
      } else if (qName.equalsIgnoreCase("trade")) {
  
      } else if (qName.equalsIgnoreCase("tradedate")) {
          btradedate = true;
      } else if (qName.equalsIgnoreCase("tradetime")) {
          btradetime = true;
      } else if (qName.equalsIgnoreCase("ec")) {
          bec = true;
      } else if (qName.equalsIgnoreCase("exch")) {
          bexch = true;
      } else if (qName.equalsIgnoreCase("pfcode")) {
          bpfcode = true;
      } else if (qName.equalsIgnoreCase("pftype")) {
          bpftype = true;
      } else if (qName.equalsIgnoreCase("pe")) {
          bpe = true;
      } else if (qName.equalsIgnoreCase("tradeqty")) {
          btradeqty = true;
      } else if (qName.equalsIgnoreCase("tradeprice")) {
          btradeprice = true;
      } 
  
  }
  
  
  @Override
  public void endElement(String uri, String localName, String qName) throws SAXException      {
      if(qName.equalsIgnoreCase("trade")) {
          tradeList.add(trd); 
      } 
  
  
  
  }
  @Override
  public void characters(char ch[], int start, int length) throws SAXException {
  
      if (bfirm) {
          //age element, set Employee age
          trd.setFirm(new String(ch, start, length));
          bfirm = false;
      } else if (bacctId) {
          trd.setAcctId(new String(ch, start, length));
          bacctId = false;
      } else if (bUserId) {
          trd.setUserId(new String(ch, start, length));
          bUserId = false;
      } else if (bseg) {
          trd.setSeg(new String(ch, start, length));
          bseg = false;
      } else if (btradedate) {
          trd.setTradedate(new String(ch, start, length));
          btradedate = false;
      } else if (btradetime) {
          trd.setTradetime(new String(ch, start, length));
          btradetime = false;
      } else if (bec) {
          trd.setEC(new String(ch, start, length));
          bec = false;
      } else if (bexch) {
          trd.setExch(new String(ch, start, length));
          bexch = false;
      } else if (bpfcode) {
          trd.setPFCode(new String(ch, start, length));
          bpfcode = false;
      } else if (bpftype) {
          trd.setPFType(new String(ch, start, length));
          bpftype = false;
      } else if (bpe) {
          trd.setPE(new String(ch, start, length));
          bpe = false;
      } else if (btradeqty) {
          trd.setTradeQty(new String(ch, start, length));
          btradeqty = false;
      } else if (btradeprice) {
          trd.setTradePrice(new String(ch, start, length));
          btradeprice = false;
         // bmore = false;
      }
  
  
  }
  } 
  </code></pre>
  
  <p>Here is an example of my xml file</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;XMLFiletoparse&gt;
  &lt;created&gt;201311290419&lt;/created&gt;
  &lt;pointInTime&gt;
      &lt;date&gt;20131129&lt;/date&gt;
      &lt;portfolio&gt;
          &lt;firm&gt;999&lt;/firm&gt;
          &lt;acctId&gt;1234G5689&lt;/acctId&gt;
          &lt;UserId&gt;11AA&lt;/UserId&gt;
          &lt;seg&gt;ABC&lt;/seg&gt;
          &lt;trade&gt;
              &lt;tradeDate&gt;20131129&lt;/tradeDate&gt;
              &lt;tradeTime&gt;08:30:00&lt;/tradeTime&gt;
              &lt;ec&gt;ABC&lt;/ec&gt;
              &lt;exch&gt;ABC&lt;/exch&gt;
              &lt;pfCode&gt;AB&lt;/pfCode&gt;
              &lt;pfType&gt;XYZ&lt;/pfType&gt;
              &lt;pe&gt;201403&lt;/pe&gt;
              &lt;tradeQty&gt;0&lt;/tradeQty&gt;
              &lt;tradePrice&gt;1.11111&lt;/tradePrice&gt;
          &lt;/trade&gt;
          &lt;trade&gt;
              &lt;tradeDate&gt;20131129&lt;/tradeDate&gt;
              &lt;tradeTime&gt;08:30:00&lt;/tradeTime&gt;
              &lt;ec&gt;ABC&lt;/ec&gt;
              &lt;exch&gt;ABC&lt;/exch&gt;
              &lt;pfCode&gt;AB&lt;/pfCode&gt;
              &lt;pfType&gt;XYZ&lt;/pfType&gt;
              &lt;pe&gt;201403&lt;/pe&gt;
              &lt;tradeQty&gt;10&lt;/tradeQty&gt;
              &lt;tradePrice&gt;2.22222&lt;/tradePrice&gt;
          &lt;/trade&gt;
          &lt;/portfolio&gt;
      &lt;portfolio&gt;
          &lt;firm&gt;888&lt;/firm&gt;
          &lt;acctId&gt;454588784KI&lt;/acctId&gt;
          &lt;UserId&gt;LMNO3&lt;/UserId&gt;
          &lt;seg&gt;ABC&lt;/seg&gt;
          &lt;trade&gt;
              &lt;tradeDate&gt;20131129&lt;/tradeDate&gt;
              &lt;tradeTime&gt;08:31:08&lt;/tradeTime&gt;
              &lt;ec&gt;ABC&lt;/ec&gt;
              &lt;exch&gt;ABC&lt;/exch&gt;
              &lt;pfCode&gt;AB&lt;/pfCode&gt;
              &lt;pfType&gt;XYZ&lt;/pfType&gt;
              &lt;pe&gt;201403&lt;/pe&gt;
              &lt;tradeQty&gt;6&lt;/tradeQty&gt;
              &lt;tradePrice&gt;3.58965&lt;/tradePrice&gt;
          &lt;/trade&gt;
  &lt;/portfolio&gt;
    &lt;/pointInTime&gt;
  &lt;/XMLFiletoparse&gt;
  </code></pre>
  
  <p>Here is portfolio class</p>
  
  <pre><code>import java.io.Serializable;
  public class Portfolio implements Serializable {
  private String date = null;
  private String firm = null;
  private String acctId = null;
  private String UserId = null;
  private String seg = null;
  private String tradedate = null;
  private String tradetime = null;
  private String ec = null;
  private String exch = null;
  private String pfcode = null;
  private String pftype = null;
  private String pe = null;
  private String tradeqty = null;
  private String tradeprice = null;
  
  public String getDate() {
      return date;
  }
  public void setDate(String date) {
      this.date = date;
  }
  public String getFirm() {
      return firm;
  }
  public void setFirm(String firm) {
      this.firm = firm;
  }
  public String getAcctId() {
      return acctId;
  }
  public void setAcctId(String acctId) {
      this.acctId = acctId;
  }
  public String getUserId() {
      return UserId;
  }
  public void setUserId(String UserId) {
      this.UserId = UserId;
  }
  public String getSeg() {
      return seg;
  }
  public void setSeg(String seg) {
      this.seg = seg;
  }
  public String getTradedate() {
      return tradedate;
  }
  public void setTradedate(String tradedate) {
      this.tradedate = tradedate;
  }
  public String getTradetime() {
      return tradetime;
  }
  public void setTradetime(String tradetime) {
      this.tradetime = tradetime;
  }
  public String getEC() {
      return ec;
  }
  public void setEC(String ec) {
      this.ec = ec;
  }
  public String getExch() {
      return exch;
  }
  public void setExch(String exch) {
      this.exch = exch;
  }
  public String getPFCode() {
      return pfcode;
  }
  public void setPFCode(String pfcode) {
      this.pfcode = pfcode;
  }
  public String getPFType() {
      return pftype;
  }
  public void setPFType(String pftype) {
      this.pftype = pftype;
  }
  public String getPE() {
      return pe;
  }
  public void setPE(String pe) {
      this.pe = pe;
  }
  public String getTradeQty() {
      return tradeqty;
  }
  public void setTradeQty(String tradeqty) {
      this.tradeqty = tradeqty;
  }
  public String getTradePrice() {
      return tradeprice;
  }
  public void setTradePrice(String tradeprice) {
      this.tradeprice = tradeprice;
  }
  
  @Override
  public String toString() {
      return "Date: " + this.date + " Firm: " + this.firm + " Acct ID: " + this.acctId + " User ID: " + this.UserId +
              " Seg: " + this.seg + " tradedate: " + this.tradedate + " tradetime: " + this.tradetime +
              " EC: " +this.ec+ " Exch: " +this.exch+ " PFCode: " +this.pfcode+ " PFType: " +this.pftype+
              " PE: " + this.pe + " Trade Qty: " + tradeqty + " Trade Price: " + tradeprice;
  }
  </code></pre>