• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to read a file on the internet?  RSS feed

 
Rodrigo Lira
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone!

Could anyone help me figuring out how to read a file on the internet?

I will have a XML file on a domain (like http://www.mydomain.com/myfile.xml) and my application will have to read this file. Actually, I will have to get the content of the file and set it to a string so that I can apply some regular expressions to retrieve data.

How can I do that with Java?

Thanks in advance.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out java.net.URL and its close friend URLConnection.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, consider treating it as XML, rather than using regex to pull out data. Unless it's trivial, short XML that's unlikely to ever change structure/format, this seems like a better solution to me.
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Check out java.net.URL and its close friend URLConnection.


They can work, but I generally use the Apache http client code,
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;

Agree with comment that for any XML longer than trivial, use a proper XML reader. Apache has one.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A year ago, for one of the tools that I wrote, I used the URL class, instead of the apache HttpClient class. The reasoning was, it was a small tool, and I didn't want to rely on any external jar files.

That tools have grown since then. It now uses both http and https. Does stuff like password challenges. URL forwarding. Connects to lots of different servers, with many different SSL configurations.... AND... the URL class has worked flawlessly, and my tool still doesn't rely on external jar files.

Henry
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:That tools have grown since then. It now uses both http and https. Does stuff like password challenges. URL forwarding. Connects to lots of different servers, with many different SSL configurations.... AND... the URL class has worked flawlessly, and my tool still doesn't rely on external jar files.


I did a similar thing, but I started about 8 years ago. Mine grew and grew, and then I decided that software reuse was more important than not using open source external jars. After all that is what ant and maven are all about.

It comes down to a make or buy decision, but I'm biased towards make, especially when the cost of "buy" is zero dollars.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I try to no re-invent the wheel unless I really need a particular wheel configuration not currently available. It happens, but rarely.

Heck, I'd usually rather have a local fork to add/modify functionality that to do it all over from scratch--I rarely have the time, and am generally a firm believer that all of us are smarter than one of us, especially when that one is me :/
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!