This article describes the method of generating XML in Java. Share it for your reference, as follows:
The generation of the drop-down box, I generated it by reading the XML file through javascript. Xml files are generated based on the database. Xml files are only equivalent to a cache from the page to the database. This is conducive to performance. Generating XML files is another tedious thing. I had to leave it to the machine to do it. The real scenario is that the program regularly triggers the program to generate xml automatically or manually. Today I separated the function generated by the xml file and wrote a small program separately.
The specific implementation is to use jxl.jar to read (I admit I like writing configurations with Excel). SQL should indicate which names are, which are codes, and which are parent codes. Mybatis querys data and assembles packets and writes them 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;/** * <pre> * XML file generator* </pre> * @author wheatmark [email protected] * @version 1.00.00 * <pre> * Modify record* Modified version: Modify by: Modify date: Modify content: * </pre> */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");//Define the variable int i,j,listSize;String filename,sqlstament,temp;;HashMap requestMap = new HashMap();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("<option>");map=(Map)r;temp = (String) map.get("DM");if(temp!=null){buf.append("<dm>"+temp+"</dm>");}temp = (String) map.get("MC");if(temp!=null){buf.append("<mc>"+temp+"</mc>");}temp = (String) map.get("PC");if(temp!=null){buf.append("<pc>"+temp+"</pc>");}temp = (String) map.get("ITEM");if(temp!=null){buf.append("<item>"+temp+"</item>");}buf.append("</option>");}CommonUtils.saveFile(null, (System.getProperty("user.dir")+"//xml//").replace("//", "/")+filename, ("<?xml version=/"1.0/" encoding=/"utf-8/" ?><root><select>"+buf.toString()+"</select></root>"),false);buf.setLength(0);}session.close();} catch (IOException e) {e.printStackTrace();}}}
The complete source code is maintained on github, address: https://github.com/ladykiller/xmlmaker.
Click here to download the complete example code.
PS: Here are a few online tools for your reference:
Online XML/JSON mutual conversion tool:
http://tools.VeVB.COM/code/xmljson
Format XML online/compress XML online:
http://tools.VeVB.COM/code/xmlformat
XML online compression/formatting tools:
http://tools.VeVB.COM/code/xml_format_compress
XML code online formatting and beautification tool:
http://tools.VeVB.COM/code/xmlcodeformat
I hope this article will be helpful to everyone's Java programming.