Get the file resource under Classpath through the Spring tool class, parse the XML

Source: Internet
Author: User

File cfgfile = Resourceutils.getfile ("Classpath:test.txt");

Or

Org.springframework.core.io.Resource Filerource = new Classpathresource ("Test.xml");

Get file: Filerource.getfile ();

Get file stream: Filerource.getinputstream ();

Get to XML: Parse: Example:<myxml> <template id= "index" type= "page" Name= "Home" templatepath= "TEST.FTL" staticpath= " Ii.html "/></myxml>

Document document = new Saxreader (). Read (Filerource.getfile ());

Gets the node with ID test for the template node below myXML
element element = (Element) Document.selectsinglenode ("/myxml/template[@id = ' test ']");

Parse the XML and get the value into the node.

String id = element.attributevalue ("id");

Get temporary files to Java:

System.getproperty ("Java.io.tmpdir"),

Windows is \,unix Yes/

File.separator

A simple test with applicationcontext, get the Bean instance (object) defined in spring. You can use:

ApplicationContext ac = new Classpathxmlapplicationcontext ("Applicationcontext.xml");
Registerdao Registerdao = (Registerdao) ac.getbean ("Registerdao", Registerdao.class);

Introduce the secondary file in the main file:

<import resource= "Classpath*:env/*/applicationcontext.xml"/>

Some properties files can be introduced into the main file as variables in the ApplicationContext

<context:property-placeholder location= "Classpath*:env/default/ecp.properties,
Classpath*:env/default/common.properties,
Classpath*:ecp.properties "
Ignore-resource-not-found= "true" ignore-unresolvable= "true"/>

Get the file resource under Classpath through the Spring tool class, parse the XML

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.