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
Rancher
Joined: Oct 14, 2008
Posts: 1337
posted
0
So the XML file is on the server? Then you can't use java.io.File 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.
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.