如何使用目标 c 从 xml 信封中提取 xml soap 正文



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

<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提取肥皂:身体。

您可以使用 XMLParser(英语:XMLParser( 提取 soap 正文。

首先将你的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 添加到类接口中。

最新更新