Java reads XML files using relative pathsBlog Category:
Javaxmljavaweb
There are three general storage locations for XML files:
1. Place under the Web-inf;
2.xml files are placed in the/web-inf/classes directory or in the Classpath jar package;
3. In a package that is similar to the Java parsing it, it is not necessarily classpath;
Two, corresponding to the use of the relative path of the Read method:
Method One: (not verified)
Place the XML file in the Web-inf directory, and then
Program code:
InputStream Is=getservletcontext (). getResourceAsStream ("/web-inf/xmlfile.xml");
Method Two: Put the XML file in the/web-inf/classes directory or classpath jar package, you can use ClassLoader static method Getsystemresourceasstream (String s) read;
Program code:
String s_xmlpath= "Com\xml\hotspot.xml";
InputStream In=classloader.getsystemresourceasstream (S_xmlpath);
Method Three: XML at random under a package path:
String s_xmlpath= "Com\xml\hotspot.xml";
ClassLoader Classloader=hotspotxmlparser.class.getclassloader ();
InputStream In=classloader.getresourceasstream (S_xmlpath);
reprinted from: http://itfafa.iteye.com/blog/1485192
Java reads XML files using relative paths