Let's say I have the following string:
NSString * str = "<a href="http://www.google.com"style="">
</br>
Come onhello world"
How can I parse out the value of the anchor tag--"Come on"?
Should I use HTMLNode.h and HTMLParser.h? Examples would be appreciated.
If it is static, then you can use string manipulation to take out the substring out of the tag
OR
If it is dynamic and html/xml code keeps changing then you can use NSXMLParser. Keep in mind that NSXMLParser should more preferably used only when data is in XML format.
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/Reference/Reference.html
Hope this helps.
Related
I have very limited experience with regex, and never used it for this type of situation, so I'm hoping someone can lead me in the right direction.
I get a link returned from a web service that will look something like this:
<a id="inventoryID-123456789" class="inventory item">See update</a>
What I need to do is create a regex that will get me back the digits in inventoryID. It will always be between 8 & 12 digits and followed by a 'class' tag.
This is using swift 4.2.
I appreciate any help.
If you are receiving a html tag and you want to get the id attribute which always has "inventoryID-<numeric id>" syntax, you could use the following pattern:
\w+-\d+
Have you tried some library for html tag parsing? It could help a lot too.
The easiest way to find an 8-12 digit numeric string is
let link = "<a id=\"inventoryID-123456789\" class=\"inventory item\">See update</a>"
if let range = link.range(of: "\\d{8,12}", options: .regularExpression) {
let inventoryID = String(link[range])
print(inventoryID)
}
I have JSON string of following format, which need to be parsed in objective-c
[{"image":"<link_to_image>",
"pic-number":"2862",
"n-lines":5,
"default":"[{"line_1":"one!"},
{"line_2":"two"},
{"line_3":"three"},
{"line_4":"four"},
{"line_5":"five"}]"
},
... ]
The length of default array may vary for each item.
Try TouchJSON which is available in:
http://code.google.com/p/touchcode/
And also go through these
How to use touch json and sbjson
how to do json parsing in iphone
How to parse JSON into Objective C - SBJSON
Just found the cause of the problem,
Instead
"default":**"**[{"line_1":"one!"},
{"line_2":"two"},
{"line_3":"three"},
{"line_4":"four"},
{"line_5":"five"}]**"**
The JSON string should has following format otherwise the parsing will fail
"default":[{"line_1":"one!"},
{"line_2":"two"},
{"line_3":"three"},
{"line_4":"four"},
{"line_5":"five"}]
I currently have an NSString which can take in a message body similar to a tweet.
E.g.
NSString *sampleText = "This text contains the link http://www.google.com"
I need to write a function that can take in this text, detect that a url exists in the string, and be able to replace the url with a placeholder text.
E.g. after the function is used, the text should equal:
sampleText = "This contains contains a LINK"
Can someone please tell me how I can do this? Do I need to use RegEx?
iOS 4 has [NSRegularExpression replaceMatchesInString:options:range:withTemplate:] which looks like a good bet. (It expects an NSMutableString.)
I'm using libxml2 for SOAP-Actions in an iPhone App.
The big problem is that i want send an SQL-Statement with
NSString *query = #"SELECT test FROM database WHERE test = \"string\""
But libxml2 converts the qotes " into "
Any ideas how to prevent this?
Quotations within the texual data of XML are supposed to be converted to ", per the XML specification. The receiver is responsible for converting " back to quotations when extracting the text from the XML.
I'm not sure about parsing an xml with attributes. have seen a similar question here
But it shows to get an attribute of intValue. But i need to get the attributes of string type,How to do that?? Images of xml and the relevant portions are given in the following links
Click here for xml and here for required data
This answer to the linked question should work for you as well. The contents of attributeDict are already NStrings. All that is going on extra in the linked answer that they are calling the intValue method on the returned NSString to parse that string into an int. In your case, you don't need this little bit of an extra step. If you just do this:
NSString * stringValue = [attributeDict objectForKey:#"attribute"];
you'll have the value of the attribute called "attribute" in a string.