So I am fairly new to working with iOS applications and I am currently working on an application which pulls data from a website using NSURLSessionDataTask, parses the JSON response (HTML), and then populates a view with data from the response.
Currently, I am stuck trying to find a solution to correctly parsing my current HTML string (of type __NSCFString/NSString) from the JSON response, and putting the text into one or more UITextViews and images into one or more UIImageViews within the main ViewController.
It has previously been suggested to simply use a UIWebView to display everything or to use an outside library to do some of the converting, however I am curious if there are any methods by which I could parse the HTML and extract all relevant text or images and throw them into an array for later use. I would very much prefer to keep all of this native and not use outside libraries unless absolutely necessary.
I have also seen some use of the NSAttributedString class for parsing HTML from a JSON response, but am unsure if this is even relevant to what I am trying to do here. Any help/suggestions/thoughts are appreciated!
You can use the native NSXMLParser class. Checkout the documentation here.
Within your custom parsing class. You can generate NSAttributedString and store into array based on custom logic. This should help.
Related
In my current application we have a service which responds with a XML.With the XML we do XSLT transformation to a HTML and display it on our web page(review,Tutorials).
My Question here is this the only way to store the content of the webpage and display it as required.
We are going to migrate to a Angular app.So do i still continue using the XSLT way or is there other better way to store the content of the page and display it.
I suggest you use native JSON communication, if possible. Or try to convert your data to a native JSON object - Angular (and javascript, really) can work much better and faster with the native JSON itself.
Although, f you want to display a html content, you can use the innerHTML tag.
If you manage to get the data in a JSON, read more and learn about the angular tehcniques of displaying data. I suggest you do the tutorial on angular.io :)
I have been in the process of converting my Android/Java app into iOS/Swift, and have run into an issue regarding table generation.
The app displays a list of content pulled from a web source as html strings, and displays these in a UITableView (RecyclerView on Android). Creating an NSMutableAttributedString with NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType works fine for basic html formatting (bold, italics, lists, etc), but I need to render tables as well.
In my Android app, I created blocks of content split at tags, and if it was a table, populated a TableView and inserted it into the correct place in the text using a LinearLayout.
The cells are programmatically generated in Swift, so I pre-process the HTML when pulled from the web and store the NSMutableAttributedStrings in an array along with the estimated heights in order to make scrolling smooth. I ran across the NSTextTable in Apple's documentation, but cannot seem to figure out how to use this class, and whether they can be embedded in an NSMutableAttributedString in order to access the table later.
If I'm totally misunderstanding what that class does, please let me know.
I am running a springboot application with Thymeleaf and reactJS. All the HTML text are read from message.properties by using th:text in the pages, but when I have th:text in reactJS HTML block, reactJS seems angry about it.
render() {
return (
<input type="text" th:text="#{home.welcome}">
)
}
The error is:
Namespace tags are not supported. ReactJSX is not XML.
Is there a walkaround besides using dangerouslySetInnerHTML?
Thank you!
There is no sane workaround.
You are getting this error because Thymeleaf outputs XML, and JSX parsers do not parse XML.
You did this because JSX looks very, very similar to XML. But they are very, very different, and even if you somehow hacked Thymeleaf to strip namespaced attributes and managed to get a component to render, it would be merely a fleeting moment of duct-taped-together, jury-rigged code that will fall apart under further use.
This is a really, really bad idea because JSX is Javascript. You are generating Javascript on the fly. Just to name a few reasons this will not work in the long term:
This makes your components difficult if not impossible to test.
Reasoning about application state will be a nightmare as you will struggle to figure out if the source of a certain state is coming from Thymeleaf or JS.
Your application will completely grind to a halt if Thymeleaf outputs bad JS.
These problems will all get worse with time (Thyme?) as as developers abuse the ease with which they can render server-side data to the client-side, leading to an insane application architecture.
Do not do this. Just use Thymeleaf, or just use React.
Sample Alternative: I primarily work on a React application backed by a Java backend. So I understand how someone could stumble upon this hybrid and think it might be a good idea. You are likely already using Thymeleaf and are trying to figure out how you can avoid rewriting your servlets but still get the power of React.
We were in a similar boat two years ago, except with an aging JSP frontend, but the difference is negligible. What we did (and it works well) is use a JSP page to bootstrap the entire React application. There is now one JSP page that we render to the user. This JSP page outputs JSON into a single <script> tag that contains some initial startup data that we would otherwise have to fetch immediately. This contains resources, properties, and just plain data.
We then output another <script> that points to the location of a compiled JS module containing the entire standalone React application. This application loads the JSON data once when it starts up and then makes backend calls for the rest. In some places, we have to use JSP for these, which is less than ideal but still better than your solution. What we do is have the JSP pages output a single attribute containing JSON. In this way (and with some careful pruning by our XHR library) we get a poor man's data interchange layer built atop a JSP framework we don't have time to change.
It is definitely not ideal, but it works well and we have benefited vastly from the many advantages of React. When we do have issues with this peculiar implementation, they are easy to isolate and resolve.
It is possible wrap ReactJS apps in Thymeleaf. Think if you want a static persistent part (like some links, or even just displayed data), you could use Thymeleaf. If you have a complicated part (something that requires DOM repaints, shared data, updates from UI/Sockets/whatever), you could use React.
If you need to pass state you could use Redux/other methods.
You could have your backend send data via a rest API to the React part and just render your simple parts as fragments or as whole chunks of plain HTML using Thymeleaf.
Remember, Thymeleaf is really just HTML. React is virtual DOM that renders as HTML. It's actually fairly easy to migrate one to the other. So you could write anything "Static" or that does not respond much to UI, in Thymeleaf/HTML. You could also just render those parts in React too, but without State.
Thymeleaf 3 allows you to render variables from your Java to a separate JS file. So that is also an option to pass into JSX
function showCode() {
var code = /*[[${code}]]*/ '12345';
document.getElementById('code').innerHTML = code;
}
Now you can use data- prefix attributes (ex. data-th-text="${message}").
https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#support-for-html5-friendly-attribute-and-element-names
I would like my webpage to display what resembles JASON Parser
does (right panel). I first installed JSONView on my Chrome.
Then I composed a string {"isbn":"asdf","name":"qwer","price":1} which I fed to JSON Parser and made sure it indeed obeyed JSON format.
Eventually, I attempted to display it on my webpage by doing:
out.println("{\"isbn\":\"asdf\",\"name\":\"qwer\",\"price\":1}\");
but it was simply displayed like any other normal String instead of being formatted like this.
How do I manage to make my webpage automatically display JSON formatted?
Thanks!
Edit: I didn't realise this was JSP. The answer I'm giving relates to using the JavaScript method JSON.stringify() which you'd somehow achieve within client-side JavaScript within your JSP project.
Use JSON.stringify() to 'pretty-print' the original JavaScript object.
For example, to have two spaces of indentation:
var str = JSON.stringify(obj, null, 2);
If this has to be achieved within Java code then you could use the GSON library. Example: Pretty-Print JSON in Java
I have a background in data and have just been getting into scraping so forgive me if my web standards and languages is not up to scratch.
I am trying to scrape some data from a javascript component of a website I use. Viewing the page source I can actually see the data I need already there within javascript function calls in JSON format. For example it looks a little like this.
<script type="text/javascript">
$(document).ready(function () {
gameState = 4;
atView.init("/Data/FieldView/20152220150142207",{"a":[{"co":true,"col:"Red"}],"b":false,...)
meLine.init([{"c":100,"b":true,...)
</script>
Now, I only need the JSON data in meLine.init. If I physically copy/paste only the JSON data into a file I can then convert that with jsonlite in R and have exactly what I need.
However I don't want to have to copy/paste multiple pages so I need a way of extracting only this data and leaving everything else behind. I originally thought to save the html source code to R, convert to text and try and regex match "meLine.init(", but I'm not really getting anywhere with that. Could anyone offer some help?
Normally I'd use XML and xpath to parse an html page but in this case (since you know the exact structure you're looking for) you might be able to do it directly with a bit of regular expressions (this is generally not a good idea as emphasized here). Not sure if this gets you exactly to your goal but
sub("[ ]+meLine.init\\((.+)\\)" , "\\1",
grep("meLine.init", readLines("file://test.html"), value=TRUE),
perl=TRUE)
will return the line you're looking for and then you can work your magic with jsonlite. The idea is to read the page line by line. grep the (hopefully) single line that contains the string meLine.init and then extract the JSON string from that. Replace file://test.html with the URL you want to use