我有这个字符串:
<?xml version="1.0" encoding="UTF-8"?>
SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:methods" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="urn:PingdomAPI" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:createAccountResponse><return xsi:type="ns2:CreateAccountResponse"><status xsi:type="xsd:int">3</status><password xsi:nil="true"/></return></ns1:createAccountResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>问题是:如何提取3的int值,即status的值。
发布于 2009-12-10 23:56:50
您需要使用NSXMLParser (或第三方解决方案)。有关更多信息和有用的包装器,请参阅this blog post of mine。
发布于 2009-12-10 23:59:45
这些例子取自苹果的event driven XML programming guide。
创建并初始化NSXMLParser实例:
BOOL success;
NSURL *xmlURL = [NSURL fileURLWithPath:pathToFile];
if (addressParser) // addressParser is an NSXMLParser instance variable
[addressParser release];
addressParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[addressParser setDelegate:self];
[addressParser setShouldResolveExternalEntities:YES];
success = [addressParser parse]; // return value not used
// if not successful, delegate is informed of error实现您感兴趣的委托方法。
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict有关委托方法的完整列表,请参见NSXMLParser Class Reference。
https://stackoverflow.com/questions/1881884
复制相似问题