Parsing XML from a string

  • I'm using NSTask to wrap an application, this application gives back
    XML. Can anybody sent me in the right direction in parsing the XML-
    response and using the elements? Do I use NSXMLParser? But this only
    seems to work with the contents of an URL or file. Is there a way
    parse XML from a string object?

    Marcel
  • Have a look at NSXMLDocument. Specifically, the
    initWithXMLString:options:error: method.

    http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes
    /NSXMLDocument_Class/Reference/Reference.html#/

    /apple_ref/occ/instm/NSXMLDocument/initWithXMLString:options:error:

    On Oct 23, 2007, at 5:22 PM, Marcel Borsten wrote:

    > I'm using NSTask to wrap an application, this application gives back
    > XML. Can anybody sent me in the right direction in parsing the XML-
    > response and using the elements? Do I use NSXMLParser? But this only
    > seems to work with the contents of an URL or file. Is there a way
    > parse XML from a string object?
    >
    > Marcel
  • Just pass your string to this using dataUsingEncoding

    - (void)deconstructXMLResponse:(NSData *)data
    {
    NSError *error;
    NSXMLDocument *responseXML = [[NSXMLDocument alloc]
    initWithData:data options:nil error:&error];
    if (!responseXML)
    {
      NSLog(@"Error reading response: %@", error);
      return;
    }
    NSLog(@"Response received: \n%@", responseXML);

    [currentRecord setResponseRaw:[[NSString alloc] initWithData:data
    encoding:NSASCIIStringEncoding]];

        // Get a reference to the root node
        NSXMLNode *rootNode = [responseXML rootElement];

    if (rootNode == nil)
        {
      NSLog(@"error: rootNode == nil");
      return;
        }
        else
    {
      NSString* tempString = [[rootNode childNamed:@"r_ONE"] stringValue];
      [currentRecord setResponseONE:tempString];
    }
    }

    On Oct 23, 2007, at 2:22 PM, Marcel Borsten wrote:

    > I'm using NSTask to wrap an application, this application gives
    > back XML. Can anybody sent me in the right direction in parsing the
    > XML-response and using the elements? Do I use NSXMLParser? But this
    > only seems to work with the contents of an URL or file. Is there a
    > way parse XML from a string object?
    >
    > Marcel
  • I tried your suggestions and they worked like a charm, thanks guys!

    Marcel

    Op 23 okt 2007, om 23:40 heeft Jim Murry het volgende geschreven:

    > Just pass your string to this using dataUsingEncoding
    >
    > - (void)deconstructXMLResponse:(NSData *)data
    > {
    > NSError *error;
    > NSXMLDocument *responseXML = [[NSXMLDocument alloc]
    > initWithData:data options:nil error:&error];
    > if (!responseXML)
    > {
    > NSLog(@"Error reading response: %@", error);
    > return;
    > }
    > NSLog(@"Response received: \n%@", responseXML);
    >
    > [currentRecord setResponseRaw:[[NSString alloc] initWithData:data
    > encoding:NSASCIIStringEncoding]];
    >
    > // Get a reference to the root node
    > NSXMLNode *rootNode = [responseXML rootElement];
    >
    > if (rootNode == nil)
    > {
    > NSLog(@"error: rootNode == nil");
    > return;
    > }
    > else
    > {
    > NSString* tempString = [[rootNode childNamed:@"r_ONE"] stringValue];
    > [currentRecord setResponseONE:tempString];
    > }
    > }
    >
    > On Oct 23, 2007, at 2:22 PM, Marcel Borsten wrote:
    >> I'm using NSTask to wrap an application, this application gives
    >> back XML. Can anybody sent me in the right direction in parsing the
    >> XML-response and using the elements? Do I use NSXMLParser? But this
    >> only seems to work with the contents of an URL or file. Is there a
    >> way parse XML from a string object?
    >>
    >> Marcel
    >
previous month october 2007 next month
MTWTFSS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Go to today