I need to call an XML file from a WCF Service and parse the content of XML in iPhone. I’m able to call the service URL but when parsing using NSXMLParser, I couldn’t get a patricular attribute value in XML. I’m using a ViewController application in XCode.
My XML file is like this:
<GetCompanyResponse xmlns="http://schemas.datacontract.org/2004/07/DomainModel"
xmlns:i="http://www.w3.org/2001/XMLSchema-
<CompanyList>
<Company>
<Id>b9ca2e32-ce88-4d72-99ce-9bc592511e85</Id>
</Company>
</CompanyList>
NSString *urlString = [NSString stringWithFormat:@"http://192.168.0.107:8732/Design_Time_Addresses/IServices/
AppointmentService/json/GetCompany";
NSURL *jsonUrl =[NSURL URLWithString:urlString];
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:jsonUrl];
[parser setDelegate:self];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
[parser release];
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:
(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"Company"]) {
NSString *name=[attributeDict objectForKey:@"Id"];
}
}
But when I check for the value in ‘name’ it is returning nil. I don’t know what’s wrong. What should I change? How can I get the value of the ID attribute?
Any help would be appreciated. Thanks in advance.
id is not attribute of the name. Put code in didEndElement:
define strVal NSString in .h;
This will work for you.