Get the right node from duplicates xml with NSXML - iphone

I'm trying to us NSXML to parse a user's channel from youtube. Parsing works ok, but I can't seem to figure out how to get the link from any specific movie as their are 5 exact the same nodes as following:
<link rel="alternate" type="text/html" href="http://www.youtube.com/watch?v=vLleTDikufefbk&feature=youtube_gdata" />
<link rel="http://gdata.youtube.com/schemas/2007#video.responses" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/vLleTDikededubk/responses" />
<link rel="http://gdata.youtube.com/schemas/2007#video.related" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/vLlededTDikubk/related" />
<link rel="http://gdata.youtube.com/schemas/2007#mobile" type="text/html" href="http://m.youtube.com/details?v=vLldedeeTDikubk" />
<link rel="self" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/users/node/uploads/vLlgrgreTDikubk" />
I've figured out how to get the attribute href from the node link. But since their are 5 different links I don't know how to only select the first one. Anyone got an idea?
Thnx you guys!!!

Found the solution already. I'll check if the link node has an attribute with alternate in it. If it does it has the right link node. Here is the code:
NSMutableArray *link; if ([elementName isEqualToString:#"link"]) if (!link) link = [[NSMutableArray alloc] init]; NSString *alternate = [attributeDict objectForKey:#"rel"]; if([alternate isEqualToString:#"alternate"]){ NSString *href = [attributeDict objectForKey:#"href"]; alternate = NULL; }

Related

Google+ Sharing - Passing hashes as parameter

In twitter, you can tell what hashtags to be included when pressing the tweet button, and that's done using the hashtags parameter.
Is there anything similar while sharing to Google+? I've read the Share documentation but found nothing like that (I care most about the Share Link method of sharing).
Do you know any ways of accomplishing that?
If your main interest is to be able to search your posts by hashtag, you can add hashtags to the snippet description. The hashtag will not be rendered differently from the rest of the text and will not be clickable though.
<html itemscope itemtype="http://schema.org/Organization">
<head>
<meta itemprop="name" content="Example">
<meta itemprop="description" content="
An #example of how to add #hashtags to the snippet description.">
<meta itemprop="image"
content="https://www.google.com/images/srpr/logo3w.png">
</head>
<body>
<div class="g-plus" data-action="share"></div>
<script type="text/javascript">
(function() {
var po = document.createElement('script');
po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(po, s);
})();
</script>
</body>
</html>

iDevice onclick not playing video with current code

My ultimate goal is to have on iDevices viewing my website, an image link that on click, plays a video at full screen, and upon finish of video redirects to another webpage. I am open to any solution that achieves my goal, even if it means scrapping the code I've got.
Here is my best attempt as of yet:
This is My current testing site
I was following this stackoverflow post
I am happy with the results on my laptop [edit works on Chrome but not FF 16.0.1 sigh I don't know anymore), but I am currently unable to click the image to play the video on my iDevices (ipad1 & iphone4). I've spent hours attempt to achieve this by researching, trial & error to no prevail.
Here is the code I am working with:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width; initial-scale=1.0" />
<meta name="description" content="" />
<title>test</title>
<script type="text/javascript">
function videoEnd() {
var video = document.getElementById("video");
video.webkitExitFullScreen();
document.location = "http://www.atxcloud.com/wp-content/uploads/Panos/beerdiaries/jk5%20all/build.html";
}
function playVideo() {
var video = document.getElementById("video");
video.addEventListener('ended', videoEnd, true);
video.webkitEnterFullScreen();
video.load();
video.play();
}
</script>
</head>
<body>
<video id="video" poster="http://www.atxcloud.com/wp-content/uploads/Panos/beerdiaries/RnD/image.png" onclick="playVideo();">
<source src="http://www.atxcloud.com/wp-content/uploads/Panos/beerdiaries/RnD/video.mp4" type="video/mp4" />
</video>
</body>
</html>
If a browser doesn't support the fullscreen API (http://caniuse.com/#feat=fullscreen) then that may throw an error in your playVideo function. Try this modification:
function videoEnd() {
var video = document.getElementById("video");
if(video.webkitExitFullScreen) video.webkitExitFullScreen();
document.location = "http://www.atxcloud.com/wp-content/uploads/Panos/beerdiaries/jk5%20all/build.html";
}
function playVideo() {
var video = document.getElementById("video");
if(video.webkitEnterFullScreen) video.webkitEnterFullScreen();
video.load();
video.play();
}
<video id="video" poster="http://www.atxcloud.com/wp-content/uploads/Panos/beerdiaries/RnD/image.png" onclick="playVideo();" onended="videoEnd();">
<source src="http://www.atxcloud.com/wp-content/uploads/Panos/beerdiaries/RnD/video.mp4" type="video/mp4" />

Showing an input that only allows numbers and the decimal point?

Is there any way to define an <input> that shows a keyboard input that only allows numbers and the decimal point (or comma for international users)?
<input type='tel'> shows phone crap I don't need, and no decimal point
<input type='number' pattern='[0-9]*'> shows all numbers, but no decimal point
What can I do to get the input I need?
You can try this attribute to your type="number" field:
pattern="\d+(\.\d*)?"
this will allow only digits with/without a decimal point and the floating numbers on the right.
Please see my project of the cross-browser filter of value of the text input element on your web page using JavaScript language: Input Key Filter . You can filter the value as an integer number, a float number, or write a custom filter, such as a phone number filter. See an example of code of input a float number
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Input Key Filter Test</title>
<meta name="author" content="Andrej Hristoliubov anhr#mail.ru">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<!-- For compatibility of IE browser with audio element in the beep() function.
https://www.modern.ie/en-us/performance/how-to-use-x-ua-compatible -->
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<link rel="stylesheet" href="https://rawgit.com/anhr/InputKeyFilter/master/InputKeyFilter.css" type="text/css">
<script type="text/javascript" src="https://rawgit.com/anhr/InputKeyFilter/master/Common.js"></script>
<script type="text/javascript" src="https://rawgit.com/anhr/InputKeyFilter/master/InputKeyFilter.js"></script>
</head>
<body>
<h1>Float field</h1>
Float field:
<input id="Float"
onchange="javascript: onChangeFloat(this)"
onblur="inputKeyFilter.isNaN(parseFloat(this.value), this);"
/>
<script>
CreateFloatFilter("Float");
function onChangeFloat(input){
inputKeyFilter.RemoveMyTooltip();
var elementNewFloat = document.getElementById("NewFloat");
var float = inputKeyFilter.parseFloat(input.value);
if(inputKeyFilter.isNaN(float, input)){
elementNewFloat.innerHTML = "";
return;
}
elementNewFloat.innerHTML = float + " or localized value: " + float.toLocaleString();
}
</script>
New float: <span id="NewFloat"></span>
</body>
</html>
Also see my page "Float field:" of the example of the input key filter
If you were doing this in a bona fide iOS app, you could probably add buttons of your own on top of the keyboard, based on this question, which is about the accessory view but the same process would work.
Otherwise, IamChuckB is right and the five keyboard types given in the Apple Developer's Library are exhaustive.
=> It will allow only single decimal dot (.)
=> It will only allow three digit decimal values after dot. you can modified by changing to {1,3} into below RegEx.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (!string.length)
return YES;
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSString *expression = #"^([0-9]+)?(\\.([0-9]{1,3})?)?$";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression
options:NSRegularExpressionCaseInsensitive
error:nil];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:newString
options:0
range:NSMakeRange(0, [newString length])];
if (numberOfMatches == 0)
return NO;
return YES;
}

Is this XPath query on parsing XHTML wrong? using TouchXML

I have been trying to parse a XHTML doc via TouchXML, but it always can't find any tags via XPath query.
Below is the XHTML:
XHTML <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content=
"HTML Tidy for Mac OS X (vers 25 March 2009), see www.w3.org" />
<title></title>
</head>
<body>
<p>
<a href="http://www.flickr.com/photos/55397648#N00/5987335786/"
title="casavermeer5.jpg by the style files, on Flickr">
<img src="http://farm7.static.flickr.com/6127/5987335786_abec990554_o.jpg"
width="500" height="750" border="0" alt="casavermeer5.jpg" />
</a>
</p>
</body>
</html>
So, we can see there are a "p" tag, "a" tag and "img" tag
What I did then is shown as the code below:
CXHTMLDocument *doc = [[[CXHTMLDocument alloc] initWithXHTMLString:XHTML options:0 error:&error] autorelease];
NSLog(#"error %#", [error localizedDescription]);
NSLog(#"doc children count = %d", [doc childCount]);
NSArray *imgNodeArray = [doc nodesForXPath:#"//img" error:&error];
NSLog(#"imgNodeArray = %d", [imgNodeArray count]);
NSLog(#"error %#", [error localizedDescription]);
The results are
error (null)
doc children count = 2
imgNodeArray = 0
error (null)
So, there are no error at all in parsing the XHTML doc and no error for the XPath query. Also this doc has two children under the root ("body" tag and "head" tag). But the problem is it cannot find the "img" tag. I have tried to replace "img" with other possible tag names (such as p, a, even body, head), no luck at all.
Can someone help me here?
P.S.
Actually the original doc is a HTML, I have used CTidy class in TouchXML lib to tidy the HTML to XHTML first. The XHTML above came from that CTidy results.
I also tried to add a namespace thing to the XPath query, like this
NSMutableDictionary *namespaceDict = [NSMutableDictionary dictionary];
[namespaceDict setValue:#"http://www.w3.org/1999/xhtml" forKey:#"xhtml"];
And change the XPath query to
NSArray *imgNodeArray = [doc nodesForXPath:#"//xhtml:img" namespaceMappings:namespaceDict error:&error];
Still no luck, can't find any results.
Try this //img.
When you use // it gets the img tag, no matter where it is in the page.
It is better than //xhtml:img - because sometimes the hierarchic tags change a bit in the code behind, so it is better to be global, and not too much specific.
I had a similar problem once that might help you. I had a document that I would parse and find certain landmarks and record their XPaths. Then, I would load the document into a UIWebView and run JavaScript to perform actions on the elements that I had previously marked. Problematically, the DOM structure was completely different after parsing the document and all my XPaths were invalid. One particular case related to tables.
<table>
<tr>
<td>Cell</td>
</tr>
</table>
The simple HTML above would always be converted to something like below. (The white space is only for readability and I'm going from memory.)
<table>
<thead></thead>
<tbody>
<tr>
<td>Cell</td>
</tr>
</tbody>
</table>
My point with this is that your parser may have injected elements into your HTML structure.

Question about regular expression evaluation using RegexKitLite

I am trying to get all css link from html like this segment of code:
<link href="http://media.ticketmaster.com/en-us/css/1c84b57773d8f594407f0b0b78d67aba/tm/default.css" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="http://media.ticketmaster.com/en-us/css/1c84b57773d8f594407f0b0b78d67aba/tm/datepicker.css"/>
<link href="http://media.ticketmaster.com/en-us/css/1c84b57773d8f594407f0b0b78d67aba/tm/carousel.css" rel="stylesheet" type="text/css" />
<link href="http://media.ticketmaster.com/en-us/css/1c84b57773d8f594407f0b0b78d67aba/tm/langoverlay_en-us.css" rel="stylesheet" type="text/css" />
Here is my code:
-(void)matchCSS:(NSString *)html{
NSString *regexString = #"href=\".*\.css\"";
NSArray *matchArray = NULL;
matchArray = [html componentsMatchedByRegex:regexString];
NSLog(#"matchArray: %#", matchArray);
}
However, what I got is a little bit crazy:
"href=\"http://media.ticketmaster.com/en-us/css/1c84b57773d8f594407f0b0b78d67aba/tm/default.css\" rel=\"stylesheet\" type=\"text/css\"",
"href=\"http://media.ticketmaster.com/en-us/css/1c84b57773d8f594407f0b0b78d67aba/tm/datepicker.css\"",
"href=\"http://media.ticketmaster.com/en-us/css/1c84b57773d8f594407f0b0b78d67aba/tm/carousel.css\" rel=\"stylesheet\" type=\"text/css\"",
"href=\"http://media.ticketmaster.com/en-us/css/1c84b57773d8f594407f0b0b78d67aba/tm/langoverlay_en-us.css\" rel=\"stylesheet\" type=\"text/css\""
These are not pure link, some of them contains some other tags that I don't want. I didn't see anything wrong with my RE. Any suggestion?
The problem is with the .*, which is too greedy.
You should match every character that is not the quote character. I am not familiar with the regular expression syntax used by RegexKitLite, but I think the regular expression should be something like #"href=\"[^\"]*\\.css\"".
You should probably use a group; in that way, the function would return you only the characters included in the group, and not all the characters matching the regular expression. If I am not wrong, the regular expression should be something like #"href=\"([^\"]*\\.css)\"", in this case.