import java.io.FileWriter;
import java.io.IOException;import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;/**
* @author zhl-wang
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class CreatXML { public static void main(String[] args) throws Exception {
Document document = DocumentHelper.createDocument();
Element root = document.addElement("root");
Element author1 =root.addElement("author").addAttribute("name", "James").addAttribute("location", "UK").addText("James Strachan");
Element author2 =root.addElement("author") .addAttribute("Wname", "Bob").addAttribute("location", "US").addText("Bob McWhirter");
Element a = author1.addElement("a");
Element b = author1.addElement("a");
Element c = author1.addElement("a");
FileWriter out = new FileWriter("foo.xml");
write(document, "foo.xml", out);
}
public static void write(Document document, String filename, FileWriter out) throws IOException {
XMLWriter writer = new XMLWriter(new FileWriter("foo.xml"));
writer.write(document);
writer.close(); OutputFormat format = OutputFormat.createPrettyPrint();
writer = new XMLWriter( System.out, format );
writer.write( document ); format = OutputFormat.createCompactFormat();
writer = new XMLWriter( System.out, format );
writer.write( document );
document.write(out); }
}
import java.io.IOException;import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;/**
* @author zhl-wang
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class CreatXML { public static void main(String[] args) throws Exception {
Document document = DocumentHelper.createDocument();
Element root = document.addElement("root");
Element author1 =root.addElement("author").addAttribute("name", "James").addAttribute("location", "UK").addText("James Strachan");
Element author2 =root.addElement("author") .addAttribute("Wname", "Bob").addAttribute("location", "US").addText("Bob McWhirter");
Element a = author1.addElement("a");
Element b = author1.addElement("a");
Element c = author1.addElement("a");
FileWriter out = new FileWriter("foo.xml");
write(document, "foo.xml", out);
}
public static void write(Document document, String filename, FileWriter out) throws IOException {
XMLWriter writer = new XMLWriter(new FileWriter("foo.xml"));
writer.write(document);
writer.close(); OutputFormat format = OutputFormat.createPrettyPrint();
writer = new XMLWriter( System.out, format );
writer.write( document ); format = OutputFormat.createCompactFormat();
writer = new XMLWriter( System.out, format );
writer.write( document );
document.write(out); }
}
解决方案 »
- 请问下数据缓存池(hibernate,mybatis)
- 在web.xml中配置struts.xml 的路径问题?
- ssh框架! 超简单的问题!困扰我好久了!
- extjs tree双击获得返回值
- 同步
- hibernate 查询方法的问题
- 如何禁掉glashfish的日志功能
- 我在jdk1。3下编写的应用程序 现在需要移植到jdk1。4 下,请问该如何处理 ?
- [提问]如何对XML进行update操作??
- org.springframework.beans.factory.CannotLoadBeanClassException 问题怎么解决
- 写了个发送邮件的程序,发布成web service 后为什么总出错?
- jbuilder9 run j2ee 时出错?
dom4j要什么JAR文件啊?