How to read and then extract HTMl source code using java program?
Hi,
Could someone tell me how to read and then extract the content of certain tag from html source code. For example, given url http://.... , I would like to know what the <Title> content <Title> in that page is.
Any help is greatly appreciate.
Use a URLConnection to make the connection to the page at the needed URL. From the URLConnection, you can get an InputStream that is the stream of data from that page. Just search through the stream and find the <title> tags (don't forget to check for case sensitivity).
Similar Messages
-
How to read and write from the serial port using java
can anyone tel me how to capture data from a serial port and display on the screen and also store it in a database.
Java Comm API, JDBC
-
How to read and parse a remote XML file with Java
Hi.
Using J2SE v1.4.2, I'd like to read and parse a remote file:
http://foo.com/file.xml
Is it possible with Java? I'd be extremely grateful if someone could provide me any webpage showing a very simple code.
Thank you very much.How about the following?
import java.io.InputStream;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
public static void main(String[] args) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
URL url = new URL("http://foo.com/file.xml");
InputStream inputStream = url.openStream();
Document document = db.parse(inputStream);
inputStream.close();
}-Blaise -
How to measure and control process CPU time of a java program
Hi dudes,
what I am trying to do is to compare 2 algorithms, with the same computation time. To measure the time, one can for example:
long startTime = System.currentTimeMillis();
while (System.currentTimeMillis() - startTime < 10 * 1000) {
running some code
However, in this case System.currentTimeMillis() measures only the wall clock time, not the real process time, i.e. if the CPU is also running some other processes at the same time, the computation time measure is inaccurate.
I learned from other post that this is not easy, but is there on earth a way to solve it? the java profiler seems to be able to monitor the process time but cannot be used to control the computation time at run time. And another thing I learned is a method Thread.getCurrentThreadCpuTime, does it measure the real thread process time or wall clock time?
Any other suggestions? I had all my codes in java and used only single thread, running under linux.
Thanks a lot in advance!
EricCheck out:
http://java.sun.com/javase/6/docs/api/java/lang/management/ThreadMXBean.html
The timing methods measure thread CPU time, not wall clock time. -
How to search and replace in an xml file using java
Hi all,
I am new to java and Xml Programming.
I have to search and replace a value Suresh with some other name in the below xml file.
Any help of code in java it is of great help,and its very urgent.
I am using java swings for generating two text boxes and a button but i am not able to search in the xml file thru the values that are entered into these text boxes.
Thanks in advance.
**XML File*
<?xml version="1.0" encoding="ISO-8859-1"?>
<student>
<stud_name>Suresh</stud_name>
<stud_age>40</stud_age>
</student>Also i am using SAX Parser in the java program
any help of code or any tutorials for sax parisng is very urgent please help me to resolve this problem
Edited by: Karthik84 on Aug 19, 2008 1:45 AM
Edited by: Karthik84 on Aug 19, 2008 3:15 AMUsing XPath to locate the elements you are after is very easy.
Try something like this:
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class BasicXMLReplaceWithDOM4J {
static String inputFile = "C:/student.xml";
static String outputFile = "C:/studentRenamed.xml";
public static void main(String[] args) throws Exception {
// Read xml and build a DOM document
Document doc = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse(new InputSource(inputFile));
// Use XPath to find all nodes where student is named 'Suresh'
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nodes = (NodeList)xpath
.evaluate("//stud_name[text()='Suresh']", doc, XPathConstants.NODESET);
// Rename these nodes
for (int idx = 0; idx < nodes.getLength(); idx++) {
nodes.item(idx).setTextContent("Suresh-Renamed");
// Write the DOM document to the file
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.transform(new DOMSource(doc), new StreamResult(new File(outputFile)));
}- Roy -
How to close a open file at OS level using Java program
I want to find a way to close a file connection at operating system (Windows 2000) level so that another program can overwrite the file. Here is the background information:
I need to schedule a job to automatically update certain files. But, when someone is accessing the file, the update will fail until I manually close the file connection at file server ( I have the system administrator privilege). I want to write a Java program that can automatically close the open file like I do manually at file server. This program can be placed on the file server in order to the job.
Does anyone know if there is any way in Java that can do the job?
ThanksIt's up to the application that opened the files to close them. There are utilities you can use to list/kill process on the machine..but you have to ask yourself WHY is this happening in the first place? It's more correct to fix the REAL problem - having your apps shut down properly - than to write a utility to close files.
That said...java alone cannot do this. You would either have to use JNI to get native access to the system or use System.Runtime to call an executable that does the job for you. -
How to read some lines from a text file using java.
hi,
i m new to java and i want to read some lines from a text file based on some string occurrence in the file. This file to be read in steps.
we only want to read the file upto the first Occurrence of "TEXT" string.
How to do it ,,,
Kinldy give the code
Regards,
Sagar
this is the text file
dfgjdjj
sfjhjkd
ghjkdg
hjkdgh TEXT
ikeyt
ujt
jk
tyk TEXT
rukl
rHendawy wrote:
Since the word "TEXT" is formed of 4 letters, you would read the text file 4 bytes by four bytes. Wrong on two counts. First, the file may not be encoded 1 byte per character. It could be utf-16 in which case it would be two byte per character. Second, even if it were 1 byte per character, the string "Text" may not start on a 4 byte boundary.
Consider a FileInputStream object "fis" that points to your text file. use fis.read(byte[] array, int offset, int len) to read every four bytes. Convert the "TEXT" String into a byte array "TEXT".getBytes(), and yous the Arrays class to compare the equality of the read bytes with your "TEXT".getBytes()Wrong since it relies on my second point and will fail when fis.read(byte[] array, int offset, int len) does not read 4 bytes (as is no guaranteed to). Check the Javadoc. Also, the file may not be encoded with the default character encoding.
The problem is easily solved by reading a line at a time using a BufferedReader wrapping an InputStreamReader wrapping a FileInputStream and specifying the correct character encoding.
Edited by: sabre150 on Apr 29, 2009 2:13 PM -
How to read/write text file to db2 by using java code
Hai
I created the table to insert a clob data as follows
db2 => create table ctest(datas clob(65536))
DB20000I The SQL command completed successfully.
db2 => describe table ctest
Column Type Type
name schema name Length Scale Nulls
DATAS SYSIBM CLOB 65536 0 Yes
1 record(s) selected.
I tried the following java code to insert a clob data.
public static synchronized Connection getConnection() throws Exception {
Connection m_connectionDatabase = null;
try {
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
// Get the connection to the database.
m_connectionDatabase = DriverManager.getConnection(
"jdbc b2 ps","","");
}catch (Exception e) {
throw new Exception("Failed to connect to the database. ", e);
return m_connectionDatabase;
public static void main(String a[]) {
File file = new File("d:\\a.txt");
InputStream fis = new FileInputStream(file);
Connection con = getConnection();
PreparedStatement pstmt = con.prepareStatement("insert into ctest values(?)");
//try 1
pstmt.setAsciiStream(1,fis,(int)file.length());
// try 2
pstmt.setCharacterStream(1,fis,(int)(file.length()));
// try 3
pstmt.setBinaryStream(1,fis,(int)(file.length()));
// try 4
byte[] b = new byte[fis.available()];
fis.read(b);
pstmt.setBytes(1, b);
// try 5
byte[] b = new byte[fis.available()];
fis.read(b);
pstmt.setString(1, new String(b));
pstmt.executeUpdate();
pstmt.close();
fis.close();
Result:
If the file size is less than 32kb, it inserted successfully.
but if more than 32kb, it throws follwing exception
COM.ibm.db2.jdbc.DB2Exception: [IBM][CLI Driver][DB2/NT] SQL0352N An unsupported SQLTYPE was encountered in position "1" of the input list (SQLDA). SQLSTATE=56084
at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.throw_SQLException(Unknown Source)
at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.throw_SQLException(Unknown Source)
at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.check_return_code(Unknown Source)
at COM.ibm.db2.jdbc.app.DB2PreparedStatement.loadParameters(Unknown Source)
at COM.ibm.db2.jdbc.app.DB2PreparedStatement.execute2(Unknown Source)
at COM.ibm.db2.jdbc.app.DB2PreparedStatement.executeUpdate(Unknown Source)
Can any body please help out to insert clob value.
Thanks in advance
V.ShanNormally I would be happy to help you, but after several years of trying to help here, I've decided not to contribute anymore because of the piss-poor way in which this site is being administered.
Others are still helping, but more may leave if things don't improve. May I recommend devshed or javaranch?
http://www.devshed.com/
http://www.javaranch.com/
If you would like to complain to the admins of this forum, either click the "Report Abuse" link or the "Feedback" link.
-- foofoo (shamelessly stolen the text from jverd). -
How to open specific port using java program
Hello,
I want to open ,close port using java comm.plz help me how can i do it.is it possible
by using java program.later i want to use that specific port to accept the server socket connection .plz
help me.i try this java program.*but it get block in accept method*.tht mean i m not able to make connection with port.
import java.sql.SQLException;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.logging.Level;
import java.util.logging.Logger;
class MakeConn
public final static int PORT = 7788;
public static java.net.Socket clientSocket = null;
public static java.io.PrintWriter pw = null; // socket output stream
public static java.io.BufferedReader br = null;
public static ServerSocket server_socket;
public static void main(String[] args) throws SQLException
try {
server_socket = new ServerSocket(PORT);
clientSocket = server_socket.accept();
System.out.println("CLIENT>>>" + clientSocket);
br = new java.io.BufferedReader(new java.io.InputStreamReader(clientSocket.getInputStream()));
pw = new java.io.PrintWriter(clientSocket.getOutputStream(), true);
String message = br.readLine().trim();
System.out.println("message is"+message);
pw.close(); // close everything
br.close();
clientSocket.close();
catch (Exception ex) {
ex.printStackTrace();
} -
How to read the html source code of a webpage.
How can I read the html source code of a webpage with a java application?
Is there a good idea?>
How can I read the html source code of a webpage
with a java application?
Is there a good idea?
I don't know if this is a good idea, but it works.
1) Use a URL to obtain the document's location
2) Use a URLConnection to open a connection between your computer and the
document server
3) Connect to the server
4) Get the InputStream of said connection
5) Associate the Input Stream with a Buffered Input Stream
At this point you can use a loop to read lines from the BufferedInput Stream and append them to a TextArea or other suitable text component. -
Extracting operators and operands from the source code
Hi there,
I am looking for a way to extract operators and operands from a source code to do a complexity analysis of the code. The final outcome I am expecting is to get the number of operators and operands in the code, to perform a Halstead Complexity Analysis.
Please let me know how this parsing can be done apart from just doing a sequential search of the content in the file.
Any samples and references are highly appreciated.
Thanks in advance!
Regards,
Aravinda777Common way to analyze code is to defining the things you are interested in using some approproate language and then using a parser generator (or compiler compiler = cc). I used antlr some time ago for this reason. Perhaps you want to take a look at it:
http://www.antlr.org/ -
I need to search a multi-page pdf and then extract just the pages returned by the search.
Version 5.0 of Preview allowed me to search a multi-paged pdf for specific text and then extract those pages returned in the search. Version 5.5 of Preview has removed that function. Does anyone know of a work-around, either using a third party piece of software, or perhaps automator? I'm desperate!
Thanks!BrooklynJohn wrote:
Version 5.5 of Preview
Sorry I can't answer your question, but I don't understand how you are using this version of Preview with Mac OS X v10.6.4. -
How to read and write a string into a txt.file
Hi, I am now using BEA Workshop for Weblogic Platform version10. I am using J2EE is my programming language. The problem I encounter is as the above title; how to read and write a string into a txt.file with a specific root directory? Do you have any sample codes to reference?
I hope someone can answer my question as soon as possible
Thank you very much.Accessing the file system directly from a web app is a bad idea for several reasons. See http://weblogs.java.net/blog/simongbrown/archive/2003/10/file_access_in.html for a great discussion of the topic.
On Weblogic there seems to be two ways to access files. First, use a File T3 connector from the console. Second, use java.net.URL with the file: protocol. The T3File object has been deprecated and suggests:
Deprecated in WebLogic Server 6.1. Use java.net.URL.openConnection() instead.
Edited by: m0smith on Mar 12, 2008 5:18 PM -
My Adobe Reader wouldn't download tickets. Tried opening Adobe Reader application but it would not open. I saw my Adobe Reader needed to be updated, so I attempted to install new Adobe Reader. When it came to finalizing, it said there is an error and can't do. Any ideas? I have no functioning Adobe reader. Should I uninstall old Adobe Reader and then try installing the updated AR?
(Had to step away & do things) I can't even open up the Adobe Reader application to uninstall; get message it won't open. And now have more things needing to be downloaded to print. Any ideas on how to open the app, so I can uninstall. I have a MAC OS X 10.9.4 When the installation initially failed I received this message from Adobe, " Adobe Reader Installation Error Package name Adobe Reader xi (11.0.07) Installer: installing abase path/installer: The install failed (The install encountered an error that caused the installation to fail. Contact the software manufacturer for assistance.) It did not specify what the error was
-
How to Read and Write .XML datas (HELP Plz...)
hai everybody
how to read and write xml datas... plz give clean and simple example..
bcoz me want to produce such type of module...
if any one help me .. thats the only way me laid in software ladder
plz....
thank u in advancethank u for giving idiot..
but before posting i search in google also..
but i cant get what me expect..
thus i posted...
then who is ................?
sorry javacoder01
// plz help me
Message was edited by:
drvijayy2k2
Maybe you are looking for
-
Can I install a 200GB HD into my G4 Quicksilver?
I have a 933MHz G4 Quicksilver with a 60GB PATA drive. I replaced the drive today with a Maxtor 200GB Ultra ATA/133 drive. But my Mac only has Ultra ATA/66 and I am worried that my drive may crash one day because I have exceeded the 128GB limit. But
-
Can anybody tell me what the difference is between the following 2 default Security reports: Rogue APs Rouge APs Event WE run both of these nightly, but the Rogue APs Event report usually is about 20 pages or so, and the information there has way mor
-
Iphone 4 scorched when charging
Last night I left my iphone 4 in my Philips DC315 'made for iphone' Alarm doc and now it is scorched at the connector and not sure it is charging anymore? Anyone else had this issue?
-
Can't load module: Out of memory or no room in system tables
Hi, I had to install openSSH on Solaris 8. By default Solaris 8 doesn't have random devices. Moreover, that machine cannot be rebooted. Hence, I chose a patch which doesn't need a reboot if the random devices are created and the /kernel/drv/random mo
-
I have downloaded the recent version i.e. iOS 7.0.2. However, I am facing an issue deleting the individual call details in Phone App.