File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Applets and the fly likes Read xml file placed on localhost Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Read xml file placed on localhost" Watch "Read xml file placed on localhost" New topic

Read xml file placed on localhost

maja neskovic
Ranch Hand

Joined: Apr 28, 2010
Posts: 134

I wanted like to read xml file and I did it like this:

But I have problem with path to my xml file. I would like to put it on my localhost in this line

so the path would be http://localhost/book.xml. I put that path in applet, but it doesn't work. I reports error, because it searches for file on this path: C:\Documents and Settings\maja.neskovic\My Documents\Princip rada steka\Stek\http:\localhost\book.xml. I am not sure where it finds this path and I hope somebody can help me to fix this path problem.
Lester Burnham

Joined: Oct 14, 2008
Posts: 1337
So the XML file is on the server? Then you can't use to get at it. You can obtain an InputStream that you can pass to dBuilder like this:

URL textURL = new URL( getCodeBase() , "book.xml" );
InputStream is = textURL.openStream()

This assumes that the XML file is in the same directory as the applet.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20180

You can't work with files when working with applets. You must use URLs instead. Applet (and therefore also JApplet) has a method called getDocumentBase(). This returns a URL to the HTML file in which the applet is embedded. You can then create a relative URL to the XML file:
Parsing doesn't change much:

Another option is to put the XML file in your JAR file and treat it as a resource. Check out Class.getResourceAsStream for more information.

Edit: beaten by Lester

Oh, and I'll move this to our Applet forum since it has nothing to do with Swing.

How To Ask Questions How To Answer Questions
maja neskovic
Ranch Hand

Joined: Apr 28, 2010
Posts: 134
I have solved the problem like you said, Rob.

Thank you both a lot

I agree. Here's the link:
subject: Read xml file placed on localhost
It's not a secret anymore!