This article mainly introduces how to generate XML in java, involves java's simple operations on xml format files, and comes with the demo source code for readers to download and reference, for more information about how to generate XML in java, see the example in this article. We will share this with you for your reference. The details are as follows:
The drop-down box is generated by reading xml files through javascript. Xml files are generated based on the database. The Xml file is only equivalent to a cache from the page to the database. This is conducive to performance. Generating xml files is a tedious task. I had to hand it over to the machine. The real scenario is that the program regularly triggers the program automatically or manually to generate xml. Today, I separated the functions generated by the xml file and wrote a small program.
The specific implementation is to use jxl. jar to read SQL statements (I admit that I like to use Execel to write configurations. The SQL statement specifies the names, codes, and parent code. Mybatis queries data and assembles messages to write data to files. This time I wrote a jar package program. Please bring your own jre before running.
Core code: XmlCreateService. java
Package com. fitweber. service; import java. io. IOException; import java. io. inputStream; import java. util. hashMap; import java. util. list; import java. util. map; import org. apache. ibatis. io. resources; import org. apache. ibatis. session. sqlSession; import org. apache. ibatis. session. sqlSessionFactory; import org. apache. ibatis. session. sqlSessionFactoryBuilder; import com. fitweber. util. commonUtils; import com. fitweber. util. execelUtils ;/**** XML file generator *
* @ Author wheatmark hajima11@163.com * @ version 1.00.00 ** Modification record * version after modification: modifier: modification date: modification content :*
*/Public class XmlCreateService {@ SuppressWarnings ({"rawtypes", "unused", "unchecked"}) public static void main (String [] argc) {String resource = "META-INF/conf/mybatis-config.xml"; String root = ""; InputStream inputStream; try {// Get the database connection inputStream = Resources. getResourceAsStream (resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder (). build (inputStream); SqlSession session = sqlSessionFactory. openSession (); // Get the query parameter List requestList = ExecelUtils. readExecelSimple ("xmlmaker.xls"); // defines the int I, j, listSize; String filename, sqlstament, temp; HashMap requestMap = new HashMap (); Map map Map; stringBuffer buf = new StringBuffer (); for (Object l: requestList) {List list = (List) l; listSize = list. size (); filename = (String) list. get (1); sqlstament = (String) list. get (2); requestMap. put ("SQL", sqlstament); List result = session. selectList ("com. fitweber. dao. xmlCreateDao. xmlDataQuery ", requestMap); for (Object r: result) {buf. append (""); Map = (Map) r; temp = (String) map. get (" DM "); if (temp! = Null) {buf. append ("
"+ Temp +"
");} Temp = (String) map. get (" MC "); if (temp! = Null) {buf. append ("
"+ Temp +"
");} Temp = (String) map. get (" PC "); if (temp! = Null) {buf. append ("
"+ Temp +"
");} Temp = (String) map. get (" ITEM "); if (temp! = Null) {buf. append ("
"+ Temp +"
");} Buf. append ("");} CommonUtils. saveFile (null, (System. getProperty ("user. dir ") +" \ xml \\"). replace ("\", "/") + filename ,("
"+ Buf. toString () +"
"), False); buf. setLength (0);} session. close ();} catch (IOException e) {e. printStackTrace ();}}}
For more articles on how to generate XML in java, please follow the PHP Chinese network!