Parsing Xml using NSXMLParser

Discussion in 'iOS Programming' started by lorend, Mar 26, 2009.

  1. lorend macrumors newbie

    Mar 26, 2009
    I have a NSString that is holding xml.

    sample: <Documents>

    What I am trying to do is parse this xml using the NSXMLParser. I see lots of examples using the initWithContentsOfURL and initWithData.

    NSXMLParser *pXML = [[NSXMLParser alloc] initWithData:yourXML];
    [pXML setDelegate:self];
    [pXML parse];

    NSXMLParser *pXML = [[NSXMLParser alloc] initWithContentsOfURL:yourURL];
    [pXML setDelegate:self];
    [pXML parse];

    When using the initWithData the parameter must be of type NSData. Is there any way to convert the NSString to NSData so it can be initialized into the parser? Or is there another way I should perform this?

    Thanks in advance.
  2. kalimba macrumors regular

    Jun 10, 2008
    You can probably do this:
    NSXMLParser *pXML = [[NSXMLParser alloc] initWithData:[COLOR="Red"](NSData *)[yourXMLString UTF8String][/COLOR]];
      	[pXML setDelegate:self];
    	[pXML parse];
    If that doesn't work, NSString has a "dataUsingEncoding:" method that returns an NSData object, but you'll have to determine the proper encoding value to pass to this method.

Share This Page