How to convert xml(soap) response into nsdictionary in swift

Discussion in 'iOS Programming' started by victorynaveen, Feb 3, 2016.

  1. victorynaveen, Feb 3, 2016
    Last edited by a moderator: Feb 3, 2016

    victorynaveen macrumors newbie

    Joined:
    Feb 3, 2016
    #1
    Help needed in swift language to convert below nsmutable string into nsdictionary.
    Code:
    //-----------nsmutablestring:
    
          <INFO>
    
        <Store>
    
        <StoreName>Colombian Jeans USA</StoreName>
    
        <StoreCity>Tamarac</StoreCity>
    
        <StoreAddress>4638 nw 58 CT</StoreAddress>     
    
        <StoreState>Florida</StoreState><
    
        StoreTelephone>(786) 290-5724</StoreTelephone>
    
        <StoreLocation>Tamarac</StoreLocation>
    
        </Store>
    
            <Store>
    
        <StoreName>Bel Sanchez Photography Inc.</StoreName>
    
        <StoreCity>Margate</StoreCity>
    
        <StoreAddress>1799 North State Road 7</StoreAddress>
    
        <StoreState>Florida</StoreState>
    
        <StoreTelephone>(954) 856-1494</StoreTelephone>
    
        <StoreLocation>Margate</StoreLocation>
    
        </Store>
    
        <Store>
    
        <StoreName>Max Computer Doctor</StoreName>
    
        <StoreCity>Tamarac</StoreCity>
    
        <StoreAddress>5575 Florida 7</StoreAddress>
    
        <StoreState>Florida</StoreState>
    
        <StoreTelephone>(954) 399-4754</StoreTelephone>
    
      <StoreLocation>Tamarac</StoreLocation>
    
            </Store>
    
            </INFO>
    





    Below format of NSDictionary data is required -
    Code:
      
    
            {
    
      
    
            Store =     (
    
      
    
                        {
    
      
    
            StoreAddress = “4638 nw 58 CT ";
    
      
    
                    StoreCity = Tamarac;
                  
                    StoreLocation = Tamarac;
    
      
    
                    StoreName = "Colombian Jeans USA";
    
      
    
                    StoreState = Florida;
    
      
    
                    StoreTelephone = "(786) 290-5724";
    
      
    
                  },
    
      
    
                        {
    
      
    
                    StoreAddress = "7868 West Sample Road";
    
      
    
                    StoreCity = Margate;
    
      
    
                    StoreLocation = Margate;
    
      
    
                    StoreName = "J.R. Pizza Bella Restaurant";
    
      
    
                    StoreState = Florida;
    
      
    
                    StoreTelephone = "(954) 753-1300";
    
      
    
                            
    
      
    
                },
    
      
    
                        {
    
      
    
                    StoreAddress = "5575 Florida 7";
    
      
    
                    StoreCity = Tamarac;
    
      
    
                    StoreLocation = Tamarac;
    
      
    
                    StoreName = "Max Computer Doctor";
    
      
    
                    StoreState = Florida;
    
      
    
                    StoreTelephone = "(954) 399-4754";
    
      
    
                              
    
      
    
                },
    
      
    
                      );
    
      
    
            "__name" = INFO;
    
      
    
            }
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    If at all possible you should prefer JSON to xml/SOAP. Is it possible to get JSON from this server?

    If not, I've used a generic XML parser to parse XML from a server that only provided XML. Your XML looks much simpler than the usual SOAP XML so this should work. A generic XML parser will parse most XML into strings, arrays, and dictionaries without any special knowledge of the XML format. The one I've used is this

    // XMLReader.m
    //
    // https://github.com/Insert-Witty-Name/XML-to-NSDictionary
    //
    // http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/

    This parser is in Objective-C but can be used in swift with no difficulties. Here is a swift github project of mine where I used this parser:

    https://github.com/phoney/WeatherMan
     
  3. victorynaveen thread starter macrumors newbie

    Joined:
    Feb 3, 2016
    #3
    Thank you so much for your help ! i able to convert that into dictionary using bridge-header file.
     
  4. page404 macrumors regular

    page404

    Joined:
    Mar 5, 2013
    Location:
    The Netherlands
    #4
    That's the fun with swift. You can wrap all those existing Objective-C libraries and use their functions. I fear the day I have to fix something in the Objective-C code though ;)
     
  5. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    This particular library is one file and not very large. I spent some time trying to port it to swift with great difficulty. The problem is that the code uses mutable containers inside containers and swift containers are structs where assignment is a copy. I assume this can be implemented in some way but I wasn't able to do it (yet).
     

Share This Page