I want to edit html pages using java. is it possible. ( it is required for my project, if I automate the test cases using selenium when it passes then the result comes in the company provided HTML sheet)
If you use JavaFX as GUI you can use its built in HTML editor.
Well there are a lot of differnt ways to edit html.
If you want to parse HTML code and manipulate it, http://jsoup.org/ is a very good solution.
Related
Without the use of any external library, what is the simplest way to fetch a website's HTML content into a string? I had tried, but I'm getting the complete page source, but I only want HTML content.
I find it a bit difficult to achieve this my friend without the use of an external lib.
You actually want to execute the javascript parts of the Html and act like a GUI-less web browser programmaticaly.
If you are to use an external library I would go for http://htmlunit.sourceforge.net/ that is pretty easy.
I'd like to take a web page and add some tags to its head. Specifically, a CSS link and a JavaScript link. I need to do this programatically for a wide variety of web pages. Now, I could hack this out with a regex or two, but I'd like to use something more robust.
What's a good way to inject or transform HTML? I'm using Scala, but anything Java or JVM will work.
You can use jsoup.
An example for modifying content in html is here
Is it possible to convert an html page with charts generated by javascript to an Image or PDF in Java?
I familiar with iText framework and it seems to be suitable but I am not sure how it handle JS generated things.
A quick search turned up this as a possible answer.
Using a library to convert to XSL-FO then another one to convert that to PDF.
Edit: This might interest you as well. There's a bit on some JBrowser class that seems to let you print web pages.
It depends on how the were generated. I suppose three possibilities:
Canvas tag.
You need to add a bit of JS code to get image using toDataURL canvas method
SVG.
You can add some code to get the full code of generated SVG document via innerHTML method.
Flash.
The worst case. I think it's hardly possible to achive what you want.
Solution 1
If you have access to plain HTML (taken after the JavaScript has executed and built the page), you can easily pass it to iText and convert it to PDF. I would recommend using Flyng-Saucer (which in turn uses iText) which has a very good and convenient API for this (See http://code.google.com/p/flying-saucer/ ).
Solution 2
On the other hand, if you do not have access to the final HTML output, you could use the Swing libraries to render the page and then take a screenshot of it. This will allow you to even use Flash, but I'm not sure whether this approach will be suitable to your problem.
However, if it is the case, you can load the web page into a Swing application (you will need to rely on a third-party browser component for JS support, but there are quite a lot out there), and then you can use the Robot class to get a screenshot of it.
Take a look at http://download.oracle.com/javase/6/docs/api/java/awt/Robot.html
I can read the HTML contents via http (for example, http://www.foo.com) using Java (with URL and BufferedReader classes). However, a couple of them contain JavaScript. My current app cannot process JavaScript.
What's the best way to read HTML content with JavaScript using Java?
I am open using other languages if it is easier.
Thanks in advance for your help.
UPDATE - Clarification:
A couple HTML contents are generated dynamically using JavaScript. I can see the result (in pure HTML after the JavaScript processing) when viewing them on a browser.
On the other hand, when my Java app retrieves the HTML contents, it says that there is no JavaScript on my app.
Ideally, I want to be able to get the same result as on the browser using my Java app.
Thanks for everyone's response.
HtmlUnit has good JavaScript support and it should (almost) parse the HTML as a web browser.
http://htmlunit.sourceforge.net/
http://htmlunit.sourceforge.net/javascript.html
Cobra (http://lobobrowser.org/cobra/getting-started.jsp) will fit your needs
For just HTML parsing you can use HTMLParser (org.htmlparser). However from the way you described your problem, it seems you need a browser, because executing is totally different than just parsing. Cheers.
With no doubt you need to use Java html parser:
Java Open Source HTML Parsers
Which Html Parser is best?
HTML/XML Parser for Java
HTML PARSER in java [closed]
Can anyone suggest me any library/jar files which I can use to export my table to excel/pdf/word.
Please tell me if there is any library by which I can create reports in jsp.
It should also be mentioned that you can export tables to Excel simply by outputting an HTML table, and setting response-type to application/vnd.ms-excel. No external libraries whatsoever needed.
Something like this:
<%# page language="java" session="true" %>
<%# taglib uri="/WEB-INF/tld/response.tld" prefix="res" %>
<res:setHeader name="Content-Type">application/vnd.ms-excel</res:setHeader>
<res:setHeader name="Content-Disposition">attachment; filename=excel-test.xls</res:setHeader>
<table>
<tr>
<td>foo</td>
<td>bar</td>
</tr>
</table>
Note: this answer is meant to supplement this and this as it covers only one of the cases (Excel).
I'd say JasperReports - which is open source - is your best bet. It would allow you to code the report once, but export it to the various formats you need. It even supports direct streaming of HTML to the browser, so it really is a code-once, use anywhere type thing. It can also scale up nicely via JasperServer.
It's different in each case.
Excel: use JExcelAPI or Apache POI;
PDF: use iText;
Word: Apache POI again.
As for creating reports, I would instead use a dedicated reporting tool, specifically Jasper Reports.
I think that itext is still better for report creation, it is more straightforward, i had some (less than enough) experience with Jasper Reports, and it seemed clumsy. OTOH itext is very easy to use for developer, and we had pretty big reports done with it, without problems.
You may even create rtf's (readable by Word) from itext.
If you'r working with JSP's you can try using displaytag library which gives you export to all (pdf, excel, csv, xml). You can also customize them or override exporters if you want.
Just take a look at this url http://displaytag.sourceforge.net/10/export.html
If your spreadsheet is very simple then exporting as CSV is acceptable; its quick and easy to code.
Docmosis and JODReports can produce PDF and DOC from the server side (JSPs, servlets, J2EE etc). Docmosis provides formatting/layout in a template so you have less coding to do and possibly even have non-developers maintaining the report look and feel. Both are free.