欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JAVA使用JDOM创建简单xml

程序员文章站 2022-03-03 17:41:48
...
package org.hu.jdom;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.jdom.*;
import org.jdom.output.XMLOutputter;

public class MyJDOM {
	public static void main(String args[]){
		Element root=new Element("root");
		Element linkman=new Element("linkman");
		Element name=new Element("name");
		Element email=new Element("email");
		Attribute id=new Attribute("id", "001");
		
		Document doc=new Document(root);
		name.setText("莉莉");
		name.setAttribute(id);
		email.setText("[email protected]");
		linkman.addContent(name);
		linkman.addContent(email);
		root.addContent(linkman);
		
		XMLOutputter out=new XMLOutputter();
		try {
			out.output(doc, new FileOutputStream(new File("D:"+File.separator+"demo.xml")));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}


转载于:https://my.oschina.net/hzy3774/blog/63077