objective-cxmlsoapkissxml

How to extract xml soap body from xml envelope using objective c?


How to extract xml soap body from xml envelope using objective c?

<soap:Envelope xmlns:c="http:************************" 
xmlns:soap="http://********************" 
xmlns:n1="http://**************" xmlns:n2="http:***********">
<soap:Header>
</soap:Header>
<soap:Body>
    <n1:GetDeviceConfiguration id="o0" c:root="1">
        <n1:request>
            <n2:Header>
                <n2:DeviceName>Tester Phone</n2:DeviceName>
                <n2:DeviceIpAddress>***********</n2:DeviceIpAddress>
                <n2:AppVersion>1.2</n2:AppVersion>
                <n2:RequestTime>2018-01-10T10:10:27</n2:RequestTime>
                <n2:MessageId>******************</n2:MessageId>
            </n2:Header>
            <n2:Body>
                <n2:LastUpdate>2017-08-06T15:34:00</n2:LastUpdate>
            </n2:Body>
        </n1:request>
    </n1:GetDeviceConfiguration>
</soap:Body>

From this xml i want to extract the soap:Body using objective C.


Solution

  • You can extract the soap Body using XMLParser.

    First convert your xml into NSData and call the parser to parse it.

        //converting into NSData
        NSData* data = [theXML dataUsingEncoding:NSUTF8StringEncoding];
    
        //initiate  NSXMLParser with this data
        NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
    
        //setting delegate
        parser.delegate = self;
    
        //call the method to parse
        BOOL result = parser.parse;
    
        parser.shouldResolveExternalEntities = true;
    

    Now, you need to implement the NSXMLParser delegate into your class.

    -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict {
    currentElement = elementName;
    NSLog(@"CurrentElementl: [%@]",elementName);
    

    }

    -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    NSLog(@"foundCharacters: [%@]",string);
    

    }

    You will find the value under key of your xml.

    Do not forget to add NSXMLParserDelegate into your class interface.