Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

lorend

macrumors newbie
Original poster
Mar 26, 2009
1
0
I have a NSString that is holding xml.

sample: <Documents>
<Document>Document1</Document>
<Document>Document2</Document>
</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.
 
I have a NSString that is holding xml.

sample: <Documents>
<Document>Document1</Document>
<Document>Document2</Document>
</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.
You can probably do this:
Code:
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.