• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Read xml file placed on localhost

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have solved the problem like you said, Rob.

Thank you both a lot

Greetings!
 
Good night. Drive safely. Here's a tiny ad for the road:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic