1

I'm consuming a Web Service using TouchXML and I'm unable to read the nodes as per the tutorial. I do receive the CXMLDocument element, however, my nodesForXPath call always returns NULL. What could be wrong?

XML Parsing code

NSMutableArray *res = [[NSMutableArray alloc] init];
NSData* valueData=[value dataUsingEncoding:NSUTF8StringEncoding];

    // Do something with the CXMLDocument* result
    CXMLDocument *result = [[[CXMLDocument alloc] initWithData:valueData options:0 error:nil] autorelease];

    NSArray *nodes = NULL;


    nodes = [result nodesForXPath:@"//Outputs" error:nil];

    NSLog (@"Nodes is %@\n", nodes);

    for (CXMLElement *node in nodes) {
        NSLog (@"Test");
        NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
        int counter;
        NSLog (@"Child Count is %@\n",[node childCount]);
        NSLog (@"Child node is%@\n",[[node childAtIndex:0] stringValue]);
        for(counter = 0; counter <= [node childCount]; counter++) {
            //  common procedure: dictionary with keys/values from XML node
            [item setObject:[[node childAtIndex:counter] stringValue] forKey:[[node childAtIndex:counter] name]];


        }


        //[item setObject:[[node attributeForName:@"id"] stringValue] forKey:@"id"];  

        [res addObject:item];
        [item release];
    }

    //  and we print our results
    NSLog(@"%@", res);
    [res release];


}

XML:

    <Nodes>
    <Inputs>
        <name>TestProgram</name>
        </Inputs>
        <Outputs>
            <ReturnCode>0</ReturnCode>
            <ReturnMessage>Success</ReturnMessage>
            <NameDetails attr1="value1">Test Program</NameDetails>
            <NameInstance attr1="value1">
            <NameCharacteristics>
                <Dob>04-25-2011</Dob>
            </NameCharacteristics>
            </NameInstance>
        </Outputs>
</Nodes>

nodesForXPatch for //Outputs always returns NULL. I want to fetch the node value. I tried replacing Outputs with Dob and still get an empty set.

Vishal Shah
  • 1,042
  • 8
  • 11
  • @vishalashah have you tried passing an NSError** to the nodesForXPath method call and then checking the returned error? – albertamg Apr 25 '11 at 18:08
  • @albertamg Yes, NSError is also nil – Vishal Shah Apr 25 '11 at 18:17
  • @vishalashah same thing on `initWithData`? Also, are you sure `valueData`contains your XML? Have you tried replacing `initWithData` with `initWithXMLString`with the XML you pasted on your question? – albertamg Apr 25 '11 at 18:34
  • @albertamg Yes, initially I started with initWithXMLString with no success. Switched to initWithData and still have the same problem. NSLog for result gives the entire XML as follows: " ....." – Vishal Shah Apr 25 '11 at 18:39
  • @vishalashah I can not see the whole XML in your last comment but I guess it's (case sensitive) equal to the one posted on your question? – albertamg Apr 26 '11 at 13:33
  • @albertamg I did not post the whole XML on purpose. I used GDataXML to overcome my parsing issues with TouchXML and everything is working as expectd. – Vishal Shah Apr 26 '11 at 15:50

0 Answers0