HTML Parsing Objective C - objective-c

<html>
<head>
<title>this value i want to grab</title>
</head>
<body> </body>
</html>
I have this NSString html, and I want to grab value in tag title?
Can anybody help me?

Use NSXMLParser, together with NSXMLParserDelegate.
In your case it is really simple. You just watch for #"title" in parser:didStartElement and in parser:foundCharacters: add the content to your own string variable, finalizing it on parser:didEndElement:.

Related

How can an nodelist created by getElementsByClassname or getElementsByTagName display its values as string

What I know about getElementsByClassName / getElementsByTagName is that both create a nodelist of the elements in question and that the nodelist elements are treated as objects I have a problem where I want to display the innerHTML of the elements inside of the nodelist but because they are objects this seems to be impossible.
Example:
<!DOCTYPE html>
<head>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<script src="javascript.js"></script>
</head>
<body>
<p id="pp"></p>
<button onclick="test()">push to test</button>
<p>dog</p>
<p>cat</p>
<p>snake</p>
</body>
//javascript.js file
function test() {
var paragraph = document.getElementsByTagName("p"),
para1 = paragraph[0].innerHTML,
ansBox = document.getElementById("pp");
ansBox.innerHTML = para1;
}
This is condensed version of a longer code. I think that the para1 variable should be a string and then the assignment statement should assign that string to the ansBox.innerHTML but instead I get nothing. I have reworked several versions of this code none work. How can you get the text elements inside of a nodelist to display in the ansBox?
Your script is loaded but your DOM hasn't loaded yet if you load your script inside head like that
<!DOCTYPE html>
<head>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body>
<p id="pp"></p>
<button onclick="test()">push to test</button>
<p>dog</p>
<p>cat</p>
<p>snake</p>
<script src="javascript.js"></script> <!-- load it here -->
</body>
Also paragraph[0] and ansBox refer to the same DOM HTMLParagraphElement just so you know which does not have anything inside (It is empty to begin with)
In the JavaScript code above, you took the HTML inside an empty element and then assign it to itself, and of course you get an empty value.

python splinter comparing unicode elementlist with string

I want to get all the anchor tag text from an iframe named "ListFirst". I'm trying to iterate text and comparing each with the string 'AGENT-WIN3E64 ' that I want to click.But the comparison I made here e['text'] == u'AGENT-WIN3E64 ' becomes false event though the strings are same. Please help.
Here is my code:
with iframe12.get_iframe('ListFirst') as iframe1231:
anchorList=iframe1231.find_by_tag('a')
for e in anchorList:
if e['text'] == u'AGENT-WIN3E64 ': #unicode string comparison
e.click()
break;
With the setup below I tried to recreate the situation you describe. The .py script below seems to find the anchor just fine though.
index.html,
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<iframe name="mainframe" src="iframe1.html"></iframe>
</body>
</html>
iframe1.html,
<html>
<head></head>
<body>
<iframe name="childframe" src="iframe2.html"></frame>
</body>
</html>
iframe2.html,
<html>
<head></head>
<body>
AGENT-WIN3E64
b
c
d
e
</body>
</html>
test.py
from splinter import Browser
browser = Browser('firefox', wait_time=10)
browser.visit("http://localhost:8000/index.html")
# get mainframe
with browser.get_iframe('mainframe') as mainframe:
# get childframe
with mainframe.get_iframe('childframe') as childframe:
anchorList = childframe.find_by_tag('a')
for e in anchorList:
if e['text'] == u'AGENT-WIN3E64 ': #unicode string comparison
print "found anchor"
e.click()
break;
This outputs,
found anchor
But note that you could also find the anchor directly using xpath,
anchor = childframe.find_by_xpath("//a[text() = 'AGENT-WIN3E64 ']")

Orchard: Displaying only a single content item

I want to be able to display a content item of a certain type and only that content item i.e. no shapes that are not part of said item.
I have tried creating a controller method with a {Themed(false)] attribute, or returning a partial view. Both of these do almost exactly what I want, except that these don't include any scripts or styles associated with the View I'm trying to display.
My current attempt look like this:
Controller method:
[Themed(false)]
public ActionResult DisplayBare(int id) {
var contentItem = _contentManager.Get(id, VersionOptions.Published);
dynamic model = _contentManager.BuildDisplay(contentItem);
return View( (object)model);
}
The DisplayBare view:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
#Display(Model)
</body>
</html>
The problem is that when the display View of an item includes Script.Require, Script.Include and Script.Foot directives, the scripts do not show up in the Html.
How would I achieve this?
Found a solution by snooping around Orchard sources:
Using this view to display my content item gives me exactly what i want:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
#{
Style.Include("Site.css");
var content = Display(Model);
}
#Display.Metas()
#Display.HeadScripts()
#Display.HeadLinks()
#Display.StyleSheetLinks()
</head>
<body>
#content
#Display.FootScripts()
</body>
</html>

VB.net Can you read the text in a certain Div

I was wondering if you read text in a certain div so when the html code says:
<html>
<head>
</head>
<body>
<div id="Main">SomeText</div>
<div id="Text">Welcome to my website</div>
</body>
</html>
i only want to see 'Welcome to my website' in the textbox 1.
is there anyone who knows how i can do that?
any help would be much appreciated.
Mark your div with runat="server":
<div id="TextDiv" runat="server">Welcome to my website</div>
then access the text in VB.NET code:
TextDiv.InnerHtml
I would recommend the HTML Agility pack hosted on codeplex at http://htmlagilitypack.codeplex.com/. With it you can connect to a HTML source, load the HTML into an reasonably friendly navigator and use XML type queries to traverse and manipulate the HTML.
I would use HtmlAgilityPack, then it's easy as:
Dim html = System.IO.File.ReadAllText("path")
Dim doc = New HtmlAgilityPack.HtmlDocument()
doc.LoadHtml(html)
Dim welcomeDiv = doc.GetElementbyId("Text")
Me.TextBox1.Text = welcomeDiv.InnerText

How to do standard layouts with StringTemplate?

With StringTemplate, what is the proper way to have a standard layout template such as:
<head>
..
</head>
<html>
$body()$
</html>
Where I can set the body template from my application, so that every template I use uses this fundamental layout?
Thanks.
I found it hiding in the documentation:
http://www.antlr.org/wiki/display/ST/StringTemplate+2.2+Documentation
"Include template whose name is
computed via expr. The argument-list
is a list of attribute assignments
where each assignment is of the form
attribute=expr. Example
$(whichFormat)()$ looks up
whichFormat's value and uses that as
template name. Can also apply an
indirect template to an attribute."
So my main layout template now looks like this:
<head>
<title>Sportello</title>
</head>
<html lang="en-US">
<body>
$partials/header()$
<section>$(body_template)()$</section>
$partials/footer()$
</body>
</html>
...to which I pass the subtemplate's name as an attribute.