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>
在实际应用中,我们可以将代码中的xml的头和尾都放在xml模板中,而代码就可以省略 StringBuffer了
原文地址:https://www.jb51.cc/xml/295497.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。