Java reads XML files using relative paths

Source: Internet
Author: User

Java reads XML files using relative pathsBlog Category:
    • Java
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.