views:

146

answers:

3

Hi Guys, I am having this string:

 [?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]

Just imagine that [ => < and ] => > The question is: How to extract int value of 3, the value for status. Thx in advance, Mladen

+1  A: 

Why the []s instead of <>s > Is it a formatting issue?

Anyway, you'll need to use NSXMLParser (or a third party solution). See this blog post of mine for some more info and a useful wrapper.

Phil Nash
+1  A: 

These examples are taken from Apple's event driven XML programming guide.

Creating and initializing a NSXMLParser instance:

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

Implement the delegation methods that are of interest to you.

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict

For a full list of delegate methods see NSXMLParser Class Reference.

rjstelling
NSXML is available only in simulator, but not on the device itself due to performance issues
Mladen
You are correct, MLaden, but rjstelling's example uses NSXMLParser, which *is* available on the device.
Phil Nash
How would I implement this to my problem. I suggest that I would init addressParser with contents of a string instead of URL. But how to extract that int value for key "status"?
Mladen
Mladen you need to implement didStartElement: and parse:foundCharacters: You really need to read the docs (and/ or my blog post). There's not too much to it. We can help you with specific issues you might have.
Phil Nash
A: 

Here where I am at the moment. I want to extract from that string values for keys "status" and "password".

Here is my code:

BOOL success;

NSXMLParser *addressParser = [[NSXMLParser alloc] initWithData:respData];
[addressParser setDelegate:self];
[addressParser setShouldResolveExternalEntities:YES];
success = [addressParser parse];

and the methods

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName

namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { if ( [elementName isEqualToString:@"status"]) { } if ( [elementName isEqualToString:@"password"]) { } }

and

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
NSLog(@"string: %@", string);

}

The last log prints out desired values for "status" and "password", I just don't know how to assign them to some variable.

Mladen