How to parse JSON/XML file into javascript? - javascript

I made a program in Java which takes an XML document, which user chooses, and present its content on screen (on JLabel using Gui). That works fine. Now, I need to create it as a web based. I want to parse a JSON file, instead of xml file, into JavaScipt. I tried to use JSP and JavaScipt in combination with HTML5 but I didn't really find the correct way to do it.
So I am wondering if there is a possible way to do it this way or if it is better to use servlets (send the data in server side).
Also, I am wondering if it is better to use the existing Java code I have, using Javabeans, instead of JavaScipt. And then combine servlets with JSP in order to print the result on screen. I know JavaScipt is quicker than Java in web applications but I think I can't see a way to make a connection between JavaScript and HTML5 to accomplish this parsing.

Related

Get the HTML source code of a web page on JavaScript

I'm building an application and I need to get the html code source of a web page in order to parse it (this web page is not on my server).
I'm coding in Javascript and I can't find a way to do it, I know there is a way to do it in Python (with requests library) and I want basicaly the same thing in Javascript.
Does someone know how to do this ?
Thanks
Try this
document.documentElement.outerHTML

Server-side template parsing

I'm currently exploring how to use HandlebarsJS via a server-side parser.
Unfortunately we are limited to using RhinoJS server.
I've searched high and low to find out how to use HandlebarsJS via the server side. I can see how to register a helper and various other things, but I'm curious to know if it's possible to parse a JS or HTML file and have the various portions of the file replaced with the server generated content?
I'm hoping this is possible without using something like node and optimistic that it's possible to parse the file just like you would do using PHP or CFML but using JS under RhinoJS instead.
Any suggestions greatly appreciated.

Create JavaScript with Umbraco

Do you know if there is a way to create a javascript or PHP file in Umbraco?
By this I mean that I would like to have the user populate fields in the same was as creating a page, but for it to output as .js or .php rather than the usual .aspx
Many thanks!
I don't see any reason why you couldn't; you could use a controller to output as pretty much anything by setting the mime-type.
I'd be wondering though what your use-case for generating PHP files would be though; the server would still need to process the PHP script somehow; which may not be a trivial task (unless you just want to generate the PHP file for download/display the code) as it isn't client-side script at all.
JavaScript on the other hand is entirely a different matter - as it's processed client-side you could just point the browser at a url that happens to deliver the generated JavaScript. You could even render the JavaScript out from a WebApi end-point and the browser client would quite happily consume it.

Use Javascript To Print Page As PDF (With Django)

I need to convert a web page to a PDF because it won't print/look correct if it isn't converted. This is because the web page is so big, that as an HTML document the browser will try and split it into multiple pages (not just vertically, which is fine, but horizontally, which is bad). Though I originally planned to do this on the server side with Django, I realized that virtually all of the available libraries were written for python2, when I was using python 3.
So my only other option is client side. The only thing I could find on stackoverflow was this: convert HTML ( having Javascript ) to PDF using JavaScript, but all of the answers were in Java, not javascript.
I think the most ideal solution would be to change the style so that it was more printer friendly rather than making it into a pdf.
If it really does have to be a pdf created with Javascript, there's a library jsPDF http://parall.ax/products/jspdf out there for creating pdfs with Javascript. You would have to write something on your own to parse the page to create a matching pdf.
If you can use php, I recommend using dompdf, which was written specifically to translate webpages into pdfs, so there would be much less work involved there. https://github.com/dompdf/dompdf I've actually used this library, and it seems decent, though it doesn't support all css styling.

Loading text file from web, and parsing it with a script?

My .html has some associated data in a .txt file that I need to load when the page is loaded. I then need a script to be able to parse that data and do stuff with it in the page. Is there a quick-n-dirty way to do this? I was thinking I could just have a hidden and then run through its innerHTML, but DIVs don't seem to support that.
Thanks in advance...new to web stuff!
EDIT: here's another catch: I want this to work, ideally, if the .html is loaded either from an actual website or from a local machine. To my knowledge, AJAX and XMLHttpRequest won't work in this situation(?)
Do you have flexibility in the format of the text file? If so I would store the data in the text file in JSON format and just include it in the HTML head of the page using script tags. Then you can use some JavaScript when the page is ready to parse the information stored in the text/JSON file and manipulate the HTML page as needed.
The nice thing about this approach is that it is simple and does not require server side code. You didn't mention whether or not the text file was dynamically generated but it doesn't matter with this approach.
You should use AJAX to load the text and a callback function to parse it according to your requirements and perform whatever actions are necessary. If you're using a framework like jQuery, look into the jQuery AJAX functions: http://docs.jquery.com/Ajax
If you're not using a framework and are instead coding straight JavaScript, look at using the XMLHttpRequest object: https://developer.mozilla.org/en/XmlHttpRequest
That should be done server side using a programming language like PHP or Perl or such. If you have to do it with JavaScript for some odd reason, the only way is to do AJAX call to the file and insert it into the DOM. There's plenty of tutorials for that.
And yes, innerHTML works on div elements, as well as with everything else.

Categories

Resources