Jdom创建xml文档(转)
来源:互联网 发布:博弈论支付矩阵画线法 编辑:程序博客网 时间:2024/06/05 09:01
//所用工具 eclipse http://www.eclipse.org
//jdom http://www.jdom.org
package com.test.jdom;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.jdom.Attribute;
import org.jdom.DocType;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.ProcessingInstruction;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class ProduceXMl {
public static void main(String[] args) throws IOException {
Document doc = new Document(); //创建空白文档
/*
* 创建PI并添加到文档
*/
Map map = new HashMap();
map.put("type","text/xsl");
map.put("href","products.xsl");
ProcessingInstruction pi = new ProcessingInstruction("xml-stylesheet",map);//处理指令
//将处理指令添加
doc.addContent(pi);
/*
* 创建文档类型并添加到文档
*/
DocType type = new DocType("productsDetails"); //文档类型
type.setPublicID("public.dtd"); //设为 public
//type.setSystemID("system.dtd"); //设为 system
//添加文档类型
doc.addContent(type);
Element root = new Element("productsDetails"); //创建一个元素
doc.setRootElement(root); //将该元素做为根元素
Element product = new Element("product");
root.addContent(product); //将product做为productsDetails的子元素
Attribute att = new Attribute("productID","0001"); //创建属性
product.setAttribute(att); //为product设置属性
//为product创建子元素,并将其content分别设为100.00,red
product.addContent(new Element("rate").setText("100.00"));
product.addContent(new Element("color").setText("红色"));
/*
* 格式化输出
*/
XMLOutputter outp = new XMLOutputter();//用于输出jdom 文档
Format format=Format.getPrettyFormat(); //格式化文档
format.setEncoding("GBK"); //由于默认的编码是utf-8,中文将显示为乱码,所以设为gbk
outp.setFormat(format);
outp.output(doc,System.out); //输出文档
}
}
结果如下:
<?xml version="1.0" encoding="GBK"?>
<?xml-stylesheet href="products.xsl" type="text/xsl"?>
<!DOCTYPE productsDetails PUBLIC "public.dtd">
<productsDetails>
<product productID="0001">
<rate>100.00</rate>
<color>红色</color>
</product>
</productsDetails>(王朝网络 wangchao.net.cn)
- Jdom创建xml文档(转)
- JDOM创建XML文档
- jdom创建XML文档
- 用jdom创建xml文档
- JDOM创建XML文档实例
- JDOM创建XML文档实例
- JDOM 创建XML(转)
- JAVA创建、读写XML文档(应用JDOM包)
- Java:使用JDOM创建XML文档
- JDOM创建XML例子(转)
- jdom学习(3)读写xml文档(转)
- jdom解析xml文档
- jdom解析xml文档
- JDOM读取XML文档
- JDOM操作XML文档
- JDOM解析XML文档
- Jdom解析XML文档
- jdom解析xml文档
- JDOM 创建XML(转)
- fdgfsdgsdf
- 咋回事儿啊
- 了解JAVA classloader
- JDom使用详解(转)
- Jdom创建xml文档(转)
- 【转】Linux USB驱动框架分析
- 【转】摄像头驱动实现源码分析
- 第一次
- 头疼的卡尔曼滤波定位
- flyinghearts《编程之美》读书笔记连载(13)
- 查看和设置tomcat内存
- 一些语句
- 新工作