dom4j ways to read XML files from a jar package _java

Source: Internet
Author: User
Tags abstract

When encapsulation, we often need to use XML to define some specifications, while running the read alone is certainly not a problem, but these XML is often put in the jar bag, so that these things can not find out. The XML definitions used in this article are as follows:

Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<ACCESOS>
<item>
<socio name= "ADSF" >
<NUMERO>00045050</NUMERO>
<REPOSICION>0</REPOSICION>
<nombre>moises moreno</nombre>
<TURNOS>
<LU>T1</LU>
<MA>T2</MA>
<MI>T3</MI>
<JU>T4</JU>
<VI>T5</VI>
<SA>T6</SA>
<DO>T7</DO>
</TURNOS>
</SOCIO>
</item>
<item>
<socio name= "ADSFA" >
<NUMERO>00045051</NUMERO>
<REPOSICION>0</REPOSICION>
<nombre>ruth pena</nombre>
<TURNOS>
<LU>S1</LU>
<MA>S2</MA>
<MI>S3</MI>
<JU>S4</JU>
<VI>S5</VI>
<SA>S6</SA>
<DO>S7</DO>
</TURNOS>
</SOCIO>
</item>
</ACCESOS>

Here's a look at the code:

Copy Code code as follows:

public void read () { 
    try { 
 &nbs p;      Document doc = new Saxreader (). Read (New File ("Src/user.xml"); 
         list<element> itemList =  doc.selectnodes ("/accesos/item/socio");  
        for (iterator<element> iter = Itemlist.iterator (); Iter.hasnext ();) { 
            element element = Iter.next (); 
            System.out.println (" Numero = "+ element.elementtext (" numero ")); 
       } 
 & nbsp; } catch (Documentexception e) { 
        e.printstacktrace ();  
   } 

This code is written in Java project, where User.xml is placed in the SRC root directory, the class under Src/com/jianxin/xml, it is obvious that running the above code can be the result, but if the above code is packaged, the directory within the jar package is
Com/jianxin/xml
User.xml
And here the method or go to src down to find user.xml, even if the src/removed from the path, still unable to locate the jar in the User.xml.
This is mainly because the jar package is a separate file, not a folder, and can never be positioned in the form of "File:/d:.../user.xml", so you cannot navigate to a file within a jar file even if it is a relative path.
Would it be an illusion to get XML into a jar package, regardless of which directory the User.xml is in, and the classes in the jar package can be found?
Of course not, we can use the class loader (ClassLoader) to do this:
1) ClassLoader is an abstract class of ClassLoader. It can dynamically get the run-time information of the load class. It can be said that when we call the ReadXml class in Xml.jar, the JVM loads into the ReadXml class and records the ReadXml run-time information, including the root path information for the jar package in which it resides. And the methods in the ClassLoader class can help us get this information dynamically:
A.public URL GetResource (String) that finds resources with the given name.
B.public InputStream getResourceAsStream (String name), returning the specified resource input stream
2) ClassLoader is abstract and cannot be instantiated. When we actually write code, it's through the GetResource and getResourceAsStream methods in class classes that delegate the GetResource and getResourceAsStream methods in the ClassLoader. Now rewrite the previous method:

Copy Code code as follows:

public void Read () {
try {
InputStream Is=this.getclass (). getResourceAsStream ("/user.xml");
Document doc = new Saxreader (). read (IS);
list<element> itemList = doc.selectnodes ("/accesos/item/socio");
for (iterator<element> iter = Itemlist.iterator (); Iter.hasnext ();) {
Element element = Iter.next ();
System.out.println ("numero =" + element.elementtext ("numero"));
}
catch (Documentexception e) {
E.printstacktrace ();
}
}

In this way, running before packaging is no problem, after the pack, still can be run to find data.
Here I feel the most important thing is to get the class at the same time will get the jar root directory information, while the ClassLoader will also change, this need to pay attention to, as to exactly what changes, at present, I also can not say, just encountered such a problem. So plan to look at a Java comparison of the bottom of the book, so that their own in the packaging for others when the heart is more at the time, has been stuck in the application level of us, it is time to delve into the JVM.
Most of the recent time is in the development, good Jira may be later, do the process, do is to use a bit of DWR, the next chapter on its description ~ ~ ~

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.