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 ... {
publicstaticvoidmain(String[]args)throwsIOException...{
Documentdoc=newDocument();//创建空白文档
/**//*
*创建PI并添加到文档
*/
Mapmap=newHashMap();
map.put("type","text/xsl");
map.put("href","products.xsl");
ProcessingInstructionpi=newProcessingInstruction("xml-stylesheet",map);//处理指令
//将处理指令添加
doc.addContent(pi);
/**//*
*创建文档类型并添加到文档
*/
DocTypetype=newDocType("productsDetails");//文档类型
type.setPublicID("public.dtd");//设为public
//type.setSystemID("system.dtd");//设为system
//添加文档类型
doc.addContent(type);
Elementroot=newElement("productsDetails");//创建一个元素
doc.setRootElement(root);//将该元素做为根元素
/**//*
加入注释
*/
root.addContent(newComment("good"));//加一条注释
/**//*
加入复杂文本
*/
CDATAdata=newCDATA("dfasfdasfdasd<adafa></adfasfsa>>SDFSAFASFDSAfa0");
root.addContent(data);
Elementproduct=newElement("product");
root.addContent(product);//将product做为productsDetails的子元素
Attributeatt=newAttribute("productID","0001");//创建属性
product.setAttribute(att);//为product设置属性
//为product创建子元素,并将其content分别设为100.00,red
product.addContent(newElement("rate").setText("100.00"));
product.addContent(newElement("color").setText("红色"));
/**//*
*格式化输出
*/
XMLOutputteroutp=newXMLOutputter();//用于输出jdom文档
Formatformat=Format.getPrettyFormat();//格式化文档
format.setEncoding("GBK");//由于默认的编码是utf-8,中文将显示为乱码,所以设为gbk
outp.setFormat(format);
outp.output(doc,System.out);//输出文档
}
}
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 ... {
publicstaticvoidmain(String[]args)throwsIOException...{
Documentdoc=newDocument();//创建空白文档
/**//*
*创建PI并添加到文档
*/
Mapmap=newHashMap();
map.put("type","text/xsl");
map.put("href","products.xsl");
ProcessingInstructionpi=newProcessingInstruction("xml-stylesheet",map);//处理指令
//将处理指令添加
doc.addContent(pi);
/**//*
*创建文档类型并添加到文档
*/
DocTypetype=newDocType("productsDetails");//文档类型
type.setPublicID("public.dtd");//设为public
//type.setSystemID("system.dtd");//设为system
//添加文档类型
doc.addContent(type);
Elementroot=newElement("productsDetails");//创建一个元素
doc.setRootElement(root);//将该元素做为根元素
/**//*
加入注释
*/
root.addContent(newComment("good"));//加一条注释
/**//*
加入复杂文本
*/
CDATAdata=newCDATA("dfasfdasfdasd<adafa></adfasfsa>>SDFSAFASFDSAfa0");
root.addContent(data);
Elementproduct=newElement("product");
root.addContent(product);//将product做为productsDetails的子元素
Attributeatt=newAttribute("productID","0001");//创建属性
product.setAttribute(att);//为product设置属性
//为product创建子元素,并将其content分别设为100.00,red
product.addContent(newElement("rate").setText("100.00"));
product.addContent(newElement("color").setText("红色"));
/**//*
*格式化输出
*/
XMLOutputteroutp=newXMLOutputter();//用于输出jdom文档
Formatformat=Format.getPrettyFormat();//格式化文档
format.setEncoding("GBK");//由于默认的编码是utf-8,中文将显示为乱码,所以设为gbk
outp.setFormat(format);
outp.output(doc,System.out);//输出文档
}
}