直接给出类文件代码:WriteXML.java<% //类的用法示例 WriteXML myxml=new WriteXML("d://shapan.xml"); myxml.setC_id("1000"); myxml.setCheck("1"); myxml.setDay("25"); myxml.setDescription("这是描述"); myxml.setMonth("5"); myxml.setName("好贵花园111"); myxml.setYear("2008"); myxml.toWrite(); myxml.toSave(); %>
package com.jh.mysv; import org.w3c.dom.*; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import java.io.*; /** * * @author Linc * @version 1.00 * @since 2008-7-21 * */ public class WriteXML { private Document document; private String filename; //private String xmlPath; /** * */ //以下是生成xml的节点元素 private String name;//楼盘名称 private String c_id;//楼盘ID private String check;//是否为现有项目楼盘 0,1 //date 日期 private String day;//日 private String month;//月 private String year;//年 private String description;//描述 /** * * @param xmlPath * {@value xml要保存的全路径+文件名} */ /* public void setXmlPath(String xmlPath) { this.xmlPath=xmlPath; } */ public void setName(String name) { this.name=name; } public void setC_id(String c_id) { this.c_id=c_id; } /** * * @param check 0 or 1 */ public void setCheck(String check) { this.check=check; } public void setDay(String day) { this.day=day; } public void setMonth(String month) { this.month=month; } public void setYear(String year) { this.year=year; } public void setDescription(String description) { this.description=description; } //name 生成xml文件名(包含全路径) public WriteXML(String name) throws ParserConfigurationException { filename=name; try { DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); document=builder.newDocument(); } catch(ParserConfigurationException pce) { pce.printStackTrace(); } } //生成所需格式的xml文档 public void toWrite() { Element root=document.createElement("shapans");//定义结点,这里是根结点 document.appendChild(root);//添加结点 //root.setAttribute("author", "linc"); Element nodeShapan=document.createElement("shapan"); root.appendChild(nodeShapan);//添加root(shapans)对象的子结点 Comment comment= document.createComment("楼盘名称"); Element nodeName=document.createElement("name"); nodeName.setTextContent(name); nodeShapan.appendChild(nodeName); //nodeName.appendChild(comment);这里屏蔽了注释 comment=document.createComment("楼盘ID"); Element nodeC_id=document.createElement("c_id"); nodeC_id.setTextContent(c_id); nodeShapan.appendChild(nodeC_id); //nodeC_id.appendChild(comment); comment=document.createComment("是否为现有项目楼盘"); Element nodeCheck=document.createElement("check"); nodeCheck.setTextContent(check); nodeShapan.appendChild(nodeCheck); //nodeCheck.appendChild(comment); Element nodeDate=document.createElement("date"); nodeShapan.appendChild(nodeDate); Element nodeDay=document.createElement("day"); nodeDay.setTextContent(day); nodeDate.appendChild(nodeDay); Element nodeMonth=document.createElement("month"); nodeMonth.setTextContent(month); nodeDate.appendChild(nodeMonth); Element nodeYear=document.createElement("year"); nodeYear.setTextContent(year); nodeDate.appendChild(nodeYear); Element nodeDescription=document.createElement("description"); nodeDescription.setTextContent(description); nodeShapan.appendChild(nodeDescription); } //保存xml文件到指定目录 public void toSave() { try { TransformerFactory tf=TransformerFactory.newInstance(); Transformer transformer=tf.newTransformer(); DOMSource source=new DOMSource(document); transformer.setOutputProperty(OutputKeys.ENCODING,"GB2312"); transformer.setOutputProperty(OutputKeys.INDENT,"yes"); PrintWriter pw=new PrintWriter(new FileOutputStream(filename)); StreamResult result=new StreamResult(pw); transformer.transform(source,result); } catch(TransformerException te) { te.printStackTrace(); } catch(IOException exp) { exp.printStackTrace(); } }// end toSave() //测试方法 public static void genShaPanXML(String xmlPath ) throws ParserConfigurationException { WriteXML myxml=new WriteXML(xmlPath); myxml.setC_id("1001"); myxml.setCheck("1"); myxml.setDay("25"); myxml.setDescription("这是描述"); myxml.setMonth("5"); myxml.setName("好贵花园"); myxml.setYear("2008"); myxml.toWrite(); myxml.toSave(); } }// end class
-------------------------------------------------------------------------------------------
下面是使用方法:
<body>
<%
//类的用法示例
WriteXML myxml=new WriteXML("d://shapan.xml");
myxml.setC_id("1000");
myxml.setCheck("1");
myxml.setDay("25");
myxml.setDescription("这是描述");
myxml.setMonth("5");
myxml.setName("好贵花园111");
myxml.setYear("2008");
myxml.toWrite();
myxml.toSave();
%>
</body>
-----------------------------------------------------------------------------------------------------------
效果如图所示!(csdn上传代码时老是出问题,晕!)