2009-12-10 155 views
0

我有這個字符串:XML解析iPhone

<?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值,對於狀態值。

回答

1

這些例子取自Apple的e vent 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

+0

由於性能問題,NSXML僅適用於模擬器,但不適用於設備本身 – Mladen 2009-12-10 16:08:24

+2

您是正確的MLaden,但rjstelling的示例使用NSXMLParser,它在設備上可用。 – philsquared 2009-12-10 16:10:39

+0

我將如何實現這個我的問題。我建議我將addressParser初始化爲字符串的內容而不是URL。但如何提取關鍵「狀態」的int值? – Mladen 2009-12-10 16:19:30