首页 > 编程语言 > 详细

JavaWeb学习笔记——JDOM

时间:2016-04-01 12:40:41      阅读:254      评论:0      收藏:0      [点我收藏+]

技术分享

 

JavaDOC的网址:http://www.jdom.org/docs/apidocs/index.html

技术分享

 

import java.io.FileOutputStream;

import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.XMLOutputter;
import org.xml.sax.Attributes;

public class WriteXML {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		//建立各个操作节点
		Element addresslist = new Element("addresslist");
		Element linkman = new Element("linkman");
		Element name = new Element("name");
		Element email = new Element("email");
		//定义属性
		Attribute id = new Attribute("id","zs"); 
		//声明一个Document对象
		Document doc = new Document(addresslist);
		//设置元素的内容
		name.setText("张三");
		name.setAttribute(id);				//设置name的属性
		email.setText("www.baidu.com");
		//设置linkman的子节点
		linkman.addContent(name);
		linkman.addContent(email);
		//将linkman加入根节点中
		addresslist.addContent(linkman);
		//用来输出XML文件
		XMLOutputter out = new XMLOutputter();
		//设置输出的编码
		out.setFormat(out.getFormat().setEncoding("UTF-8"));
		//输出XML文件
		try{
			out.output(doc, new FileOutputStream("/home/common/software/coding/HelloWord/JavaWeb/bin/address.xml"));
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}

 技术分享

技术分享

JavaWeb学习笔记——JDOM

原文:http://www.cnblogs.com/tonglin0325/p/5344399.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!