使用jdom创建xml文档

jdom是什么?
dom是w3c为了兼容各语言而推出的一种结构中立的标准,他的使用并不符合java程序员的习惯
jdom是一种使用java编写的,基于dom的符合java程序员习惯的xml操作工具

使用jdom创建并保存xml文件

package com.dowebber.jdomxmlcreater;

import java.io.File;
import java.io.PrintWriter;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

public class JdomXMLCreater {

 public static void main(String[] args) {
  
  //创建文档对象
  Element root = new Element("students");
  Document doc = new Document(root);
 
  //添加字段
  Element student = new Element("student");
  Element studentname = new Element("name");
  Element studentsex = new Element("sex");
  studentname.addContent("周哥哥");
  studentsex.addContent("男");
  student.addContent(studentname);
  student.addContent(studentsex);
  root.addContent(student);
  
  File file = new File("d:\\students.xml");
  try{
   PrintWriter pw = new PrintWriter(file);
   XMLOutputter out = new XMLOutputter();
   out.output(doc, pw);
  }catch(Exception e){
   e.printStackTrace();
  }
 }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值