1

I am trying to parse thE following string that i receive from my WebService using SOAP.. Since i am not able to parse the data i receive from my web service( of which i have no idea why.. please take a look here parsing XML using Soap Web Services) i am converting the data to an NSString and then using TOUCHXML to parse the string

NSString *theXML = [[NSString alloc] 
                    initWithBytes: [webData mutableBytes] 
                    length:[webData length] 
                    encoding:NSUTF8StringEncoding];

the string that i get is

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">

<s:Body>

<GetCategoryResponse xmlns="http://tempuri.org/">

<GetCategoryResult>

<Root>

<Record>

<catid>1</catid><catname>Hi</catname><catlogo>http://10.25.4.49/monsoonstore/Category/50X50.GIF</catlogo>

</Record>

</Root>

</GetCategoryResult>

</GetCategoryResponse>

</s:Body>

</s:Envelope>

since i am not able to get any parser to read the NSData i get from webservice i convert the above string to the following (ie. the exact xml i needed)

<Root><Record><catid>1</catid><catname>Hi</catname><catlogo>http://10.25.4.49/monsoonstore/Category/50X50.GIF</catlogo></Record></Root>

CXMLDocument *doc = [[CXMLDocument alloc] initWithXMLString:check options:0 error:nil];



  NSArray *items = [doc nodesForXPath:@"//Root" error:nil];




for (CXMLElement *item in items) 

{


NSLog(@"Root");

    NSArray *titles = [item elementsForName:@"Record"];

for(CXMLElement *title in titles) 
{
    NSLog(@"Record");

     break;
}

 NSArray *categories = [item elementsForName:@"catid"];

    for(CXMLElement *category in categories)
    {
        NSLog(@"catid");

        break;
    }


NSArray *artists = [item elementsForName:@"catname"];

    for(CXMLElement *artist in artists) 
    {
        NSLog(@"catname");

        break;
        }
.
.
.
.

The Root and Record logs get called while the catid never gets called.. what am i possibly doing wrong??

Community
  • 1
  • 1
hemant
  • 1,771
  • 4
  • 31
  • 43

2 Answers2

0

Use NSXMLParser, As you specify you need a parser which could be created by NSData directly.

NSXMLParser have the initWithData that return the NSXMLParser instance by sending NSData as parameter.

- (id)initWithData:(NSData *)data

Parsing XML on the iPhone using NSXMLParser.

Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
  • i did that exactly but NSXMLParser is not able to parse the correct xml tags instead of reading **** as the first tag it reads the **** as the first tag and then my whole xml gets parsed even with the tags and the < > sign.. it doesn't recognize the **root** or **record** tags – hemant Jun 04 '11 at 09:36
0

I think you are searching catid under records (and not under root) So change

 NSArray *categories = [item elementsForName:@"catid"];

to

NSArray *categories = [titles elementsForName:@"catid"];

I mean, search element catid under records, not under root.

My above code could give you compile error, as i havent worked on TouchXML, but you know what i mean.