使用JDOM创建和解析XML文件

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

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

/**
 * 使用JDOM创建XML文件
 *
 * @author sageparadise
 */
public class WriteOderXml {
 public static void CreateXml() throws FileNotFoundException, IOException {
  // 创建元素
  Element orderRoot = new Element("订单");
  // 为根元素添加属性
  orderRoot.setAttribute("创建人", "admin");
  // 文档对象 就是xml文件在内存中存在的形式
  Document doc = new Document(orderRoot);
  // 创建根元素的第一个子元素
  Element addressee = new Element("A收件人");
  orderRoot.addContent(addressee);
  // 创建第一个子元素的子元素
  Element name = new Element("A姓名");
  name.setText("马燕飞");
  addressee.addContent(name);
  Element street = new Element("A街道");
  street.setText("王府井46#");
  addressee.addContent(street);
  Element city = new Element("A城市");
  city.setText("北京");
  addressee.addContent(city);
  Element duchy = new Element("A直辖市");
  duchy.setText("北京");
  addressee.addContent(duchy);
  Element post = new Element("A邮编");
  post.setText("1000001");
  // 创建根元素的第二个子元素
  Element ddate = new Element("订货日期");
  ddate.setText("2000-12-1");
  orderRoot.addContent(ddate);
  // 创建根元素的第三个子元素
  Element fdate = new Element("发货日期");
  fdate.setText("2000-12-19");
  orderRoot.addContent(fdate);
  // 创建根元素的第四个子元素
  Element remark = new Element("备注");
  remark.setText("日常用品系列");
  orderRoot.addContent(remark);
  // 创建根元素的第五个子元素
  Element ware = new Element("货物单");
  orderRoot.addContent(ware);
  // 创建子元素的子元素
  Element goods = new Element("商品");
  ware.addContent(goods);
  Element gname = new Element("名称");
  gname.setText("紫罗兰");
  goods.addContent(gname);
  Element grade = new Element("等级");
  grade.setText("A");
  goods.addContent(grade);
  Element price = new Element("价格");
  price.setText("20");
  goods.addContent(price);
  Element goods1 = new Element("商品");
  ware.addContent(goods1);
  Element gname1 = new Element("名称");
  gname1.setText("霸王洗发水");
  goods1.addContent(gname1);
  Element grade1 = new Element("等级");
  grade1.setText("A");

  goods1.addContent(grade1);
  Element price1 = new Element("价格");
  price1.setText("50");
  goods1.addContent(price1);

  // 设置显示格式
  Format format = Format.getRawFormat();
  format.setIndent("    ");
  XMLOutputter out = new XMLOutputter(format.setEncoding("UTF-8"));
  out.output(doc, new FileOutputStream(new File("D://updir//order.xml")));
 }

 public static void main(String[] agrs) throws FileNotFoundException,
   IOException {
  WriteOderXml order = new WriteOderXml();
  order.CreateXml();

 }

}

 

 

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

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

/**
 * 使用JDOM创建XML文件
 *
 * @author sageparadise
 */
public class WriteOderXml {
 public static void CreateXml() throws FileNotFoundException, IOException {
  // 创建元素
  Element orderRoot = new Element("订单");
  // 为根元素添加属性
  orderRoot.setAttribute("创建人", "admin");
  // 文档对象 就是xml文件在内存中存在的形式
  Document doc = new Document(orderRoot);
  // 创建根元素的第一个子元素
  Element addressee = new Element("A收件人");
  orderRoot.addContent(addressee);
  // 创建第一个子元素的子元素
  Element name = new Element("A姓名");
  name.setText("马燕飞");
  addressee.addContent(name);
  Element street = new Element("A街道");
  street.setText("王府井46#");
  addressee.addContent(street);
  Element city = new Element("A城市");
  city.setText("北京");
  addressee.addContent(city);
  Element duchy = new Element("A直辖市");
  duchy.setText("北京");
  addressee.addContent(duchy);
  Element post = new Element("A邮编");
  post.setText("1000001");
  // 创建根元素的第二个子元素
  Element ddate = new Element("订货日期");
  ddate.setText("2000-12-1");
  orderRoot.addContent(ddate);
  // 创建根元素的第三个子元素
  Element fdate = new Element("发货日期");
  fdate.setText("2000-12-19");
  orderRoot.addContent(fdate);
  // 创建根元素的第四个子元素
  Element remark = new Element("备注");
  remark.setText("日常用品系列");
  orderRoot.addContent(remark);
  // 创建根元素的第五个子元素
  Element ware = new Element("货物单");
  orderRoot.addContent(ware);
  // 创建子元素的子元素
  Element goods = new Element("商品");
  ware.addContent(goods);
  Element gname = new Element("名称");
  gname.setText("紫罗兰");
  goods.addContent(gname);
  Element grade = new Element("等级");
  grade.setText("A");
  goods.addContent(grade);
  Element price = new Element("价格");
  price.setText("20");
  goods.addContent(price);
  Element goods1 = new Element("商品");
  ware.addContent(goods1);
  Element gname1 = new Element("名称");
  gname1.setText("霸王洗发水");
  goods1.addContent(gname1);
  Element grade1 = new Element("等级");
  grade1.setText("A");

  goods1.addContent(grade1);
  Element price1 = new Element("价格");
  price1.setText("50");
  goods1.addContent(price1);

  // 设置显示格式
  Format format = Format.getRawFormat();
  format.setIndent("    ");
  XMLOutputter out = new XMLOutputter(format.setEncoding("UTF-8"));
  out.output(doc, new FileOutputStream(new File("D://updir//order.xml")));
 }

 public static void main(String[] agrs) throws FileNotFoundException,
   IOException {
  WriteOderXml order = new WriteOderXml();
  order.CreateXml();

 }

}

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值