使用JDOM创建和解析XML文件
2011-04-17 19:29
489 查看
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();
}
}
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();
}
}
相关文章推荐
- Java&Xml教程(六)使用JDOM解析XML文件
- 在JSP开发中使用jdom解析临时存放数据的XML文件
- PHP使用SimpleElement创建和解析xml文件
- 使用JDOM解析XML文件
- 使用PHP DOM-XML创建和解析XML文件
- 使用dom4j创建和解析xml文件
- 使用JDOM解析xml文件
- C++使用tinyxml创建和解析xml文件(本文用于cocos2dx)
- 使用PHP DOM-XML创建和解析XML文件
- 使用Dom4j创建与解析XML文件
- 使用jdom解析xml文件和xml格式的字符串
- 使用jdom解析XML文件之saxbuilder用法
- cocos2dx使用tinyxml创建和解析xml文件
- 使用tinyxml创建及解析XML文件
- Java&Xml教程(六)使用JDOM解析XML文件
- 利用jdom解析xml配置文件,并且按照xml格式输出,进行缩进
- XML:使用DOM技术解析xML文件中的城市,实现select级联选择
- Python之使用ElementTree解析xml文件
- Java进阶(二十七)使用Dom4j解析XML文件