JDOM 生成XML字符串 博客分类: JAVA bytearrayoutputstreamxml
程序员文章站
2024-02-24 21:04:22
...
package com.abin.xml.test; import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.IOException; import org.jdom.Document; import org.jdom.Element; import org.jdom.output.XMLOutputter; public class CreateXML1 { public static String doXML()throws FileNotFoundException,IOException{ Element root=new Element("school"); Document doc=new Document(root); for(int i=0;i<5;i++){ Element element=new Element("people"); element.setAttribute("id", ""+i); element.addContent(new Element("username").setText("abin")); element.addContent(new Element("password").setText("varyall")); element.addContent(new Element("age").setText("12")); root.addContent(element); } ByteArrayOutputStream byteRsp=new ByteArrayOutputStream(); XMLOutputter xmlOut=new XMLOutputter(); try { xmlOut.output(doc, byteRsp); } catch (Exception e) { // TODO: handle exception } String result=byteRsp.toString(); return result; } public static void main(String[] args)throws IOException{ String result=new CreateXML1().doXML(); System.out.println("result="+result); } }