title: Complex Conversion XML to Dictionary (Swift / iOS)
body: <p>I'm racking my brain how to convert this parsed xml into arrays or dictionaries.  the xml tags are not helpful because the labels are generic and there are ~10 headers.  I might be able to do something based on the order of the labels.  any ideas?</p>
  
  <p>NSXMLParser Method Code:</p>
  
  <pre><code>class MyXMLParserDelegate: NSObject, NSXMLParserDelegate {
  
  @objc func parserDidStartDocument(parser: NSXMLParser) {
      print("parserDidStartDocument")
  }
  
  @objc func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
      print("didStartElement       --&gt; \(elementName)")
  }
  
  @objc func parser(parser: NSXMLParser, foundCharacters string: String) {
      print("foundCharacters       --&gt; \(string)")
  }
  
  @objc func parser(parser: NSXMLParser, didEndElement elementName: String,
                    namespaceURI: String?, qualifiedName qName: String?) {
      print("didEndElement         --&gt; \(elementName)")
  }
  
  @objc func parser(parser: NSXMLParser, didStartMappingPrefix prefix: String,
                    toURI namespaceURI: String) {
      print("didStartMappingPrefix --&gt; Prefix: \(prefix) toURI: \(namespaceURI)")
  }
  
  @objc func parser(parser: NSXMLParser, didEndMappingPrefix prefix: String) {
      print("didEndMappingPrefix   --&gt; Prefix: \(prefix)")
  }
  
  @objc func parserDidEndDocument(parser: NSXMLParser) {
      //reload table with array
      print("parserDidEndDocument")
  }
  }
  </code></pre>
  
  <p>Sample results of XML parsing using NSXMLParser methods:</p>
  
  <pre><code>&lt;result&gt;
   &lt;header&gt;
      &lt;col&gt;
        &lt;label&gt;Tree Name&lt;/label&gt;
      &lt;/col&gt;
      &lt;col&gt;
        &lt;label&gt;Num Levels&lt;/label&gt;
      &lt;/col&gt;
      &lt;col&gt;
        &lt;label&gt;Defaults Weight&lt;/label&gt;
      &lt;/col&gt;
      &lt;col&gt;
        &lt;label&gt;Name&lt;/label&gt;
      &lt;/col&gt;
      &lt;col&gt;
        &lt;label&gt;Abbrev&lt;/label&gt;
      &lt;/col&gt;
      &lt;col&gt;
        &lt;label&gt;Level&lt;/label&gt;
      &lt;/col&gt;
      &lt;col&gt;
        &lt;label&gt;Full Name&lt;/label&gt;
      &lt;/col&gt;
    &lt;/header&gt;
    &lt;body&gt;
      &lt;row&gt;
        &lt;col&gt;Cost Center 1&lt;/col&gt;
        &lt;col&gt;2&lt;/col&gt;
        &lt;col&gt;5&lt;/col&gt;
        &lt;col&gt;Miami Dolphins Front Office&lt;/col&gt;
        &lt;col/&gt;
        &lt;col&gt;0&lt;/col&gt;
        &lt;col/&gt;
      &lt;/row&gt;
      &lt;row&gt;
        &lt;col&gt;Cost Center 1&lt;/col&gt;
        &lt;col&gt;2&lt;/col&gt;
        &lt;col&gt;5&lt;/col&gt;
        &lt;col&gt;Accounts Receivable&lt;/col&gt;
        &lt;col&gt;A/R&lt;/col&gt;
        &lt;col&gt;1&lt;/col&gt;
        &lt;col&gt;Accounts Receivable&lt;/col&gt;
      &lt;/row&gt;
      &lt;row&gt;
        &lt;col&gt;Cost Center 1&lt;/col&gt;
        &lt;col&gt;2&lt;/col&gt;
        &lt;col&gt;5&lt;/col&gt;
        &lt;col&gt;06&lt;/col&gt;
        &lt;col&gt;06&lt;/col&gt;
        &lt;col&gt;1&lt;/col&gt;
        &lt;col&gt;06&lt;/col&gt;
      &lt;/row&gt;
      &lt;row&gt;
        &lt;col&gt;Cost Center 2&lt;/col&gt;
        &lt;col&gt;3&lt;/col&gt;
        &lt;col&gt;5&lt;/col&gt;
        &lt;col&gt;Cost Center 2&lt;/col&gt;
        &lt;col/&gt;
        &lt;col&gt;0&lt;/col&gt;
        &lt;col/&gt;
      &lt;/row&gt;
      &lt;row&gt;
        &lt;col&gt;Cost Center 2&lt;/col&gt;
        &lt;col&gt;3&lt;/col&gt;
        &lt;col&gt;5&lt;/col&gt;
        &lt;col&gt;test2&lt;/col&gt;
        &lt;col/&gt;
        &lt;col&gt;1&lt;/col&gt;
        &lt;col&gt;test2&lt;/col&gt;
      &lt;/row&gt;
      &lt;row&gt;
        &lt;col&gt;Cost Center 2&lt;/col&gt;
        &lt;col&gt;3&lt;/col&gt;
        &lt;col&gt;5&lt;/col&gt;
        &lt;col&gt;test&lt;/col&gt;
        &lt;col/&gt;
        &lt;col&gt;1&lt;/col&gt;
        &lt;col&gt;test&lt;/col&gt;
      &lt;/row&gt;
      &lt;row&gt;
        &lt;col&gt;Cost Center 3&lt;/col&gt;
        &lt;col&gt;3&lt;/col&gt;
        &lt;col&gt;5&lt;/col&gt;
        &lt;col&gt;Cost Center 3&lt;/col&gt;
        &lt;col/&gt;
        &lt;col&gt;0&lt;/col&gt;
        &lt;col/&gt;
      &lt;/row&gt;
      &lt;row&gt;
        &lt;col&gt;Cost Center 3&lt;/col&gt;
        &lt;col&gt;3&lt;/col&gt;
        &lt;col&gt;5&lt;/col&gt;
        &lt;col&gt;test&lt;/col&gt;
        &lt;col/&gt;
        &lt;col&gt;1&lt;/col&gt;
        &lt;col&gt;test&lt;/col&gt;
      &lt;/row&gt;
    &lt;/body&gt;
    &lt;footer/&gt;
  &lt;/result&gt;
  </code></pre>
  
  <p>parserDidStartDocument</p>
  
  <p>didStartElement       --> result</p>
  
  <p>foundCharacters       --> </p>
  
  <p><em>didStartElement       --> header</em></p>
  
  <p>foundCharacters       --> </p>
  
  <p>didStartElement       --> col</p>
  
  <p>foundCharacters       --> </p>
  
  <p>didStartElement       --> label</p>
  
  <p><strong>foundCharacters       --> Tree Name</strong></p>
  
  <p>didEndElement         --> label</p>
  
  <p>foundCharacters       --> </p>
  
  <p>didEndElement         --> col</p>
  
  <p>foundCharacters       --> </p>
  
  <p>didStartElement       --> col</p>
  
  <p>foundCharacters       --> </p>
  
  <p>didStartElement       --> label</p>
  
  <p><strong>foundCharacters       --> Num Levels</strong></p>
  
  <p>didEndElement         --> label</p>
  
  <p>foundCharacters       --> </p>
  
  <p>didEndElement         --> col</p>
  
  <p>foundCharacters       --> </p>
  
  <p>didStartElement       --> col</p>
  
  <p>foundCharacters       --> </p>
  
  <p>didStartElement       --> label</p>
  
  <p><strong>foundCharacters       --> Defaults Weight</strong></p>
  
  <p>didEndElement         --> label</p>
  
  <p>foundCharacters       --> </p>
  
  <p>didEndElement         --> col</p>
  
  <p>foundCharacters       --> </p>
  
  <p>didStartElement       --> col</p>
  
  <p>foundCharacters       --> </p>
  
  <p>didStartElement       --> label</p>
  
  <p><strong>foundCharacters       --> Name</strong></p>
  
  <p>didEndElement         --> label</p>
  
  <p>foundCharacters       --> </p>
  
  <p>didEndElement         --> col</p>
  
  <p>foundCharacters       --> </p>
  
  <p>didStartElement       --> col</p>
  
  <p>foundCharacters       --> </p>
  
  <p>didStartElement       --> label</p>
  
  <p><strong>foundCharacters       --> Abbrev</strong></p>
  
  <p>didEndElement         --> label</p>
  
  <p>foundCharacters       --> </p>
  
  <p>didEndElement         --> col</p>
  
  <p>foundCharacters       --> </p>
  
  <p>didStartElement       --> col</p>
  
  <p>foundCharacters       --> </p>
  
  <p>didStartElement       --> label</p>
  
  <p><strong>foundCharacters       --> Level</strong></p>
  
  <p>didEndElement         --> label</p>
  
  <p>foundCharacters       --> </p>
  
  <p>didEndElement         --> col</p>
  
  <p>foundCharacters       --> </p>
  
  <p>didStartElement       --> col</p>
  
  <p>foundCharacters       --> </p>
  
  <p>didStartElement       --> label</p>
  
  <p><strong>foundCharacters       --> Full Name</strong></p>
  
  <p>didEndElement         --> label</p>
  
  <p>foundCharacters       --> </p>
  
  <p>didEndElement         --> col</p>
  
  <p>foundCharacters       --> </p>
  
  <p><em>didEndElement         --> header</em></p>
  
  <p>foundCharacters       --> </p>
  
  <p><em>didStartElement       --> body</em></p>
  
  <p>foundCharacters       --> </p>
  
  <p>didStartElement       --> row</p>
  
  <p>foundCharacters       --> </p>
  
  <p>didStartElement       --> col</p>
  
  <p><strong>foundCharacters       --> Cost Center 1</strong></p>
  
  <p>didEndElement         --> col</p>
  
  <p>foundCharacters       --> </p>
  
  <p>didStartElement       --> col</p>
  
  <p><strong>foundCharacters       --> 2</strong></p>
  
  <p>didEndElement         --> col</p>
  
  <p>foundCharacters       --> </p>
  
  <p>...</p>