如何使用 objective c 从 xml 信封中提取 xml 皂体?
How to extract xml soap body from xml envelope using objective c?
如何使用 objective c 从 xml 信封中提取 xml 皂体?
<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 我想使用 objective C.
提取 soap:Body
您可以使用 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;
现在,您需要在 class 中实现 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 添加到您的 class 界面中。
如何使用 objective c 从 xml 信封中提取 xml 皂体?
<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 我想使用 objective C.
提取 soap:Body您可以使用 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;
现在,您需要在 class 中实现 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 添加到您的 class 界面中。