response.setContentType("text/xml;charset=GB2312");
request.setCharacterEncoding("ISO-8859-1");
java.io.PrintWriter out = response.getWriter();
out.println(xml内容);
request.setCharacterEncoding("ISO-8859-1");
java.io.PrintWriter out = response.getWriter();
out.println(xml内容);
* 创建日期 2005-3-31
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package test;import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.Document;
import org.w3c.dom.Element;/**
* @author Administrator
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class Test7 {
private static Document doc=null; public static void main(String[] args) throws ParserConfigurationException {
Test7 test=new Test7();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder(); doc = builder.newDocument();
Element books = doc.createElement("Books"); Element book = doc.createElement("Book");
book.setAttribute("id", "001");
book.setAttribute("name", "WangPeng");
book.appendChild(doc.createTextNode("Thinking in java")); books.appendChild(book);
doc.appendChild(books);
try {
test.saveDOMToXML("c:\\aa.xml",doc);
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
private String otputDOMToXMLString(Document doc) throws IOException {
String XMLString = "";
//建立dom的输出格式
OutputFormat format = new OutputFormat(doc);
StringWriter stringOut = new StringWriter();
XMLSerializer serial = new XMLSerializer(stringOut, format);
serial.asDOMSerializer();
serial.serialize(doc.getDocumentElement());
XMLString = stringOut.toString();
System.out.println(XMLString);
return XMLString;
}
private void saveXMLString(String XMLString, String fileName)
throws IOException {
File file = new File(fileName);
if (file.exists()) {
file.delete();
}
file.createNewFile();
if (file.canWrite()) {
FileWriter fileOut = new FileWriter(file);
fileOut.write(XMLString);
fileOut.close();
}
} public void saveDOMToXML(String filePath, Document doc) throws IOException {
String XMLString = null;
XMLString = this.otputDOMToXMLString(doc); //把document转为字符串型
this.saveXMLString(XMLString, filePath); //保存为xml格式
}
}
DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.newDocument();
Element books = doc.createElement("Books"); Element book = doc.createElement("Book");
book.setAttribute("id", "001");
book.setAttribute("name", "WangPeng");
book.appendChild(doc.createTextNode("Thinking in java")); books.appendChild(book);
doc.appendChild(books);
//((XmlDocument) doc).write(new FileOutputStream(new File("dom.xml")));
((XmlDocument) doc).write(out);
你把这些语句放到try catch块里,看看是不是在往jsp输出的时候抛出异常了~~~
总觉得你这句话又问题:((XmlDocument) doc).write(out);