PDA

View Full Version : Substring? or Parsing XML?




ilx.mac
Apr 10, 2009, 06:41 AM
Hi there,

I went through the various substring methods like
NSString *str1 = [theResponseString substringFromIndex:2]
NSString *pathTrimmed = [theResponseString lastPathComponent];

and also other methods from:

http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/cl/NSString

But none of them fits my requirement.

My String:

Thisismyentirestringwithhtmltagtowardstheend<html><head><title>My Page</title></head><body>My web page</body></html>

All I have to do is to get the complete html tag out of that string. Also, the position of the <HTML> tag will vary from time to time.

Is that possible, can some one post their suggestions/code samples please?

If not, I have to do it other way. I have an xml:

<tag1>
<method>
<id>001</id>
<name>james</name>
<![CDATA[ <html><head><title>My Page</title></head><body>My web page</body></html> ]]>
</method>
</tag1>

I have to parse the xml & retrieve the CDATA alone. I have written the parser method, but it fails to read the CDATA alone. It reads the <id>& <name> tags. Also all the other are inside some other node. But CDATA is not inside any node. I know that it is the problem. But i cant add a tag to that.

Is there any way to parse the CDATA & retrieve the XML?

Thanks in Advance!



ilx.mac
Apr 10, 2009, 08:29 AM
I did it some other way using NSScanner:

NSString *stringBeforeHtml = nil, *theRest = nil, *temp = nil, *str = @"SomeString<html></html>";
NSScanner *scanner = [[NSScanner alloc] initWithString:str];

if([scanner scanUpToString:@"<html>" intoString:&stringBeforeHtml]) {

// Get all the rest:
if(![scanner isAtEnd])
theRest = [str substringFromIndex:[scanner scanLocation]];

// Or get everything until we have a </html>
if(![scanner isAtEnd]) {
[scanner scanUpToString:@"</html>" intoString:&theRest];
[scanner scanString:@"</html>" intoString:&temp];
theRest = [NSString stringWithFormat:@"%@%@", theRest, temp];
}
}

[scanner release];

and it worked.;)

blackmesa
Apr 10, 2009, 08:34 AM
Try NSXMLParser here (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/Reference/Reference.html)