Executable jar package program with arguments reads XML from the specified location and resolves the specified field

Source: Internet
Author: User

1, requirements Description: A group of XML files containing table DDL information, the same structure, want to quickly read the table structure information

2, the implementation of the environment: Linux/unix

3, the realization of the idea: A new Java project, the main function to set two parameters, one is the file path, one contains the table structure information node attribute name, Java program execution time with the parameters, directly to parse the results of a string of the way to print out

4, XML file format reference:

<?xml version= "1.0" encoding= "UTF-8"?> <beans> <bean class= " Org.springframework.jdbc.datasource.DriverManagerDataSource "id=" Table_bas "> <property name=" Db2tabname " > <value>sor. ldapd_yixin_msg</value> </property> <property name= "Oracleddl" > <value> (clndr_dt_id number ( (a), Std_prvnce_cd varchar2 (a), city_id varchar2 (a), pd_inst_id varchar2 (a), Col_date varchar2 (a), Col_uid (10 ), Col_phone varchar2, Col_os varchar2 (a), Col_carrier varchar2 (a), Col_country varchar2 (MB), col_province VARCHAR2 (a), col_city varchar2 (m), col_feed number (a), Col_voiceleft number (a), col_vlduration number (a), Col_ TEXT number, Col_audio number, Col_video number (a), col_image number (a), col_lbs number (a), Col_card number (19 ), col_emotion number (a), Col_music number (a), col_sms number (a), Col_nat_call number (a), col_new_teams number (a), C OL_READFC Number (5), col_login_cnt number (a), file_clndr_dt_id number (a), crtd_tmst Date, Work_dt date) </value> </property> <property name= "Datafileseqlist" > <value> 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,4 4,45,46,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63</value> </property> </bean> </beans >
5, Java Code reference: (Required in the engineering properties--java build path--libraries to introduce the external jar package Jdom.jar)

Import Java.io.File;
Import java.io.IOException;

Import java.util.List;
Import org.jdom.Document;
Import org.jdom.Element;
Import org.jdom.JDOMException;


Import Org.jdom.input.SAXBuilder; public class Tableddlbyxml {public static void main (String args[]) throws Jdomexception, IOException {Saxbuilder Bui
		Lder = new Saxbuilder (false);
		String Path = args[0];//file path + filename Document doc = (document) builder.build (new file (path));
		Element root = Doc.getrootelement (); list<element> list = Root.getchildren (); all child element Bean {//beans of//root:beans for (Element i:list) list<elemen t> element = I.getchildren (); All child elements of//bean property for (element Property:element) {//Get the name attribute
	                        Value String name = Property.getattributevalue ("name");
	                            if (Name.equals (args[1]))//oracleddl or COMMENTDDL {//Get child element capacity text value String value = Property.getChildtext ("value");
	                        System.out.println (value); }
        	       }
		}
	}
}
6. Play jar package: To make an executable jar package, a lower version of Eclipse may not have this feature.

On the project right-click--export, select runnable jar file instead of jar file, as shown below, click Next:


Local run will have the execution configuration information, in the next interface can be directly selected this configuration, which contains the specified main function Java class. For the main method to enter parameters, when configuring run information, the parameters are written in program arguments, and multiple parameters are separated by spaces. The next configuration interface is shown below:


The Library handling is specifically for referencing external jar packages, with three options that have the following meanings:

1 Extract required libraries into generated jar: All used class files (including their own and third-party packages), into a jar package, third party in the new jar package directly in the class mode of storage

2 Package required libraries into generated jar: Make your own class file into a jar package, and put the Third-party jar bag into the new jar bag intact, The third party jar package's class has to undergo two decompression to see that the Third-party jar package is not visible to the JVM in this way, and Eclipse is accessed in a special way.

3 Copy required libraries into a sub-folder next to the generated jar: Make your own class file into a jar package and place the third party jar package in a subfolder next to it.

The general choice of the first item on it, and then click Finish, package completed.

7, the implementation of the way on the Linux reference:

If you put the jar bundle and the XML file in the same directory, and the jar's executable permissions are open, you can execute it directly with the following script:

Java-jar./tablebas.jar SOR. Ldapd_yixin_msg.20141221.m.xml ORACLEDDL

The first parameter after the jar package file is the XML file name, the second parameter is the node property name of the table structure information, and the table structure is printed directly after execution.

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.