Get your CodeRanch badge!*
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Read xml file placed on localhost" Watch "Read xml file placed on localhost" New topic
Author

Read xml file placed on localhost

maja neskovic
Ranch Hand

Joined: Apr 28, 2010
Posts: 134
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

Joined: Oct 14, 2008
Posts: 1337
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

Joined: Oct 27, 2005
Posts: 19552
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
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

Greetings!
 
Don't get me started about those stupid light bulbs.
 
subject: Read xml file placed on localhost
 
Similar Threads
JAVA DOM API to read xml
Connection refused: connect in faces-config.xml
Updating Xml Element values
help with web service to display xml file contents using netbeans
how to read this kind of xml file?