• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Read xml file placed on localhost

 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

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
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have solved the problem like you said, Rob.

Thank you both a lot

Greetings!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic