Get URL from html page - objective - iphone

I need to get URL from loaded HTML page. Here is HTML tag where placed my URL
<a class="top_nav_link" id="logout_link" href="https://login.vk.com/?act=logout&hash=29327318c645d49a48&from_host=vk.com&from_protocol=http" onclick="if (!checkEvent(event)) { ge('logout_form').submit(); return false; }">
And URL: "https://login.vk.com/?act=logout&hash=29327318c645d49a48&from_host=vk.com&from_protocol=http"
Hash could be different.
How to get this URL?

Since you say the HTML is actually well-formed XHTML, then you can use any XML parsing method to parse the document and find what you are looking for. Using NSXMLParser and a valid parser delegate, you probably would have something like:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:#"a"] && [[attributeDict objectForKey:#"id"] isEqualToString:#"logoutLink"]) {
// Found the <a> tag with an id of logoutLink
NSString *linkURL = [attributeDict objectForKey:#"href"];
// Do what you want with the link URL here
}
This assumes you are looking for a specific <a> element with an id of logoutLink. If you are looking for other ways to identify which <a> tag has the URL you want you can adjust the if statement in this sample code accordingly.

Related

Difficulty parsing RSS feed

I'm trying to parse an RSS feed (http://www.themostunrealbeats.com/?feed=rss2) using NSXMLParser. I am having difficulty finding the picture in the article. Here is where the picture is in the RSS feed.
<media:content url="http://themostunrealbeats.files.wordpress.com/2012/03/madeon.png?w=400" medium="image">
<media:title type="html">madeon</media:title>
</media:content>
Specifically, I want http://themostunrealbeats.files.wordpress.com/2012/03/madeon.png. Yet in the delegate method for NSXMLParser, I don't find anything.
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ([element isEqualToString:#"media:content"]) {
NSLog(#"%#", string);
[content appendString:string];
}
}
string has no value. How can I parse this?
// NSXMLParser has a following method
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
// In this method parameter 'attributeDict' will return you all the sub attributes of main attribute.
// In your case its 'url' of Picture.
// I hope this will help you. Check this out.

Parsing XML file with same name attributes in iphone using NSXMLParser?

I am creating an iPhone App in which I have to consume values from a service url.The response from url is of XML format.XML file has same name attributes many times,and those attributes are not static.I mean the no of attributes may increase from time to time. So I couldn't understand how to consume that XML response in iPhone.
My XML response looks like this :
GetUserResponse xmlns="http://schemas.datacontract.org/2004/07"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<CompanyList>
<User>
<Address>
<City>Alabaster</City>
<State>Alabama</State>
</Address>
<Employee>
<Name>DrJohn</Name>
</Employee>
</User>
<User>
<Address>
<City>SanFransisco</City>>
<State>California</State>
</Address>
<Employee>
<Name>DrWilliams</Name>
</Employee>
</User>
</CompanyList>
</GetUserResponse>
The thing is I couldn't say there will be specific number of tags as of 2 tags here.They may or may not increase from time to time.I think we should take something like count number of items and extract the values but couldn't understand how?
This is one of many ways to do it. You can simply get the attributeDict and parse all the values from it using key-value.
NSString *element;
- (void) viewDidLoad {
[super viewDidLoad];
cityArr = [[NSMutableArray alloc] init];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
element = elementName;
if ([elementName isEqualToString:#“Address”]) {
// initialize stings to store values of child elements
cityStr = [NSMutableString alloc] init];
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if ([element isEqualToString:#“City”])
cityStr appendString:string];// Append Values
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:#“Address”]) {
[cityArr addObject:cityStr]; // Add strings to array/dictionary and release string
}
}

Unable to Parse following XML?

I have continuous tags of img id="#", where # varies from 1 to 9.
The description of tags consists of floating values.
When I use the standard NSXML Parser, i not getting all the values.
My XML for reference:
<img id="1">-0.0111328,-0.0635608,0.152549,0.11211,-0.0250431,
-0.0370875,0.0862391,0.0970791,-0.0195908,
-0.00892297,0.0791795,0.0554013,0.00362028,0.0138572,0.0432729,
0.0253036,-0.0770325,0.14065,0.118424,0.1787,
0.0734354,0.160883,0.101831,0.237038,0.0681151,0.178331,
0.106532,0.224731,0.133766,0.222096,0.165214,0.240752,
-0.0280366,0.106239,0.052094,0.110642,
</img>
How would I parse the above XML?
Kindly, help me out.
Thanx
This is because parser:foundCharacters: does not deliver all characters at once. You need to concatenate all strings that you get between the callbacks of the parser:didStartElement:namespaceURI:qualifiedName:attributes: and parser:didEndElement:namespaceURI:qualifiedName: that you get for the <img> tag.
In the code below, buf is an NSMutableString ivar of your parser delegate.
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
if ([qualifiedName isEqualToString:#"img"]) {
buf = [NSMutableString string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([qualifiedName isEqualToString:#"img"]) {
buf = [buf stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(#"Got %#", buf);
}
}
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
[buf appendString:string];
}
Finally got it... i placed start and end tags for img id = '#'. My structure now looks like this:
<images>
<img id = '1'> -0.0111328,-0.0635608,0.152549,0.11211,-0.0250431,
-0.0370875,0.0862391,0.0970791,-0.0195908,
-0.00892297,0.0791795,0.0554013,0.00362028,0.0138572,0.0432729,
0.0253036,-0.0770325,0.14065,0.118424,0.1787,
0.0734354,0.160883,0.101831,0.237038,0.0681151,0.178331,
0.106532,0.224731,0.133766,0.222096,0.165214,0.240752,
-0.0280366,0.106239,0.052094,0.110642, ....
</img>
<img id = '2'> ...
</img>
....
....
</images>
<mapping>
<map>
<imgid> 1 </imgid>
<keyword> heavy </keyword>
</map>
<map>
<imgid> 2 </imgid>
<keyword> metal </keyword>
</map>
...
...
</mapping>
Placing start and end tags allowed me to parse the whole xml.
Earlier, the start and end tags were for individual images which only resulted in parsing of one img.
This made me add another key point while parsing XML.
Hope this helps others as well.

How to get the attributes inside a tag in an html file on iOS?

Here is my link i want to extract the youtube videos out from the html at this URL
http://gdata.youtube.com/feeds/api/users/mizzoushape/uploads?orderby=updated
$<link rel ='alternate' href='somelink.html'/>$
Can any one help me out how to fetch the attributes inside the tag..
Thanks in advance for your valuable time
If you are parsing XML File Then:
In the parser:didStartElement method, the attributes are stored in the attributeDict dictionary. The values are then stores with the attribute name as the key
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{
// Here you can get value from "attributeDict" by key for example
NSString *strHref = [attributeDict objectForKey:#"href"];
}

How to get a part of tag in XML (iOS)

How can I get some data from the tag of a xml file?
I have a tag like this:
`<link href="http://127.0.0.1:8580/directory/playlists/" rel="alternate" type="application/atom+xml;type=feed" />`
I would like to save the http link: http://127.0.0.1:8580/directory/playlists/
Can I use the NSXMLParser?
Thanks a lot!
You need to implement a delegate for the NSXMLParser that conforms to the NSXMLParserDelegate protocol. In this implementation implement at least this method:
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:#"link"]) {
NSString* href = [attributeDict objectForKey:#"href"];
// Do you stuff with the href
}
}
The value you are after "http://127.0.0.1:8580/directory/playlists/" is called an attribute. It is the attribute of the element "link".
Check out the Apple documentation relating to XML processing and elements and attributes. For example, http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/XMLParsing/Articles/HandlingElements.html