如何使用目标c从xml信封中提取xml肥皂体?

时间:2018-02-01 05:18:30

标签: objective-c xml soap kissxml

如何使用目标c从xml信封中提取xml soap body?

<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>

从这个xml我想要使用目标C提取soap:Body。

1 个答案:

答案 0 :(得分:0)

您可以使用XMLParser提取soap Body。

首先将xml转换为NSData并调用解析器来解析它。

    //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;

现在,您需要在您的类中实现NSXMLParser委托。

-(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);

}

您将在xml的密钥下找到该值。

不要忘记将 NSXMLParserDelegate 添加到您的类界面中。