java 代码如下:
public class GeneralXML { public static String readFile(String filePath) { StringBuffer sb = new StringBuffer(); File file = new File(filePath); FileReader fr = null; try { fr = new FileReader(file); } catch (FileNotFoundException e) { e.printStackTrace(); } BufferedReader br = new BufferedReader(fr); String strLine = ""; try { while ((strLine = br.readLine()) != null) { sb.append(strLine); sb.append("\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { fr.close(); br.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } public static void main(String[] args) { final String head = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"; final String root_start = "<root>"; final String root_end = "</root>"; StringBuffer sb = new StringBuffer(); sb.append(head); sb.append(root_start); MessageFormat mf = new MessageFormat(readFile("src/main/resources/template.xml")); sb.append(mf.format(new Object[] { "11","12","13","14"})); sb.append(mf.format(new Object[] { "21","22","23","24"})); sb.append(root_end); System.out.println(sb.toString()); } }
xml模板如下:
<study name="{0}"> <one>{1}</one> <two>{2}</two> <three>{3}</three> </study>
原文链接:https://www.f2er.com/xml/295497.html