我有个xml类型的字符串 怎么生成生成xml文件
解决方案 »
- 一对多关系单向关系中多的一端怎么插入数据
- Java 操作Properties 读写问题
- 问下各位大大,在北京5K---6K的java程序员要会到什么程度?
- 生成的xml为什么没有缩进
- 该面试了,各位大哥大姐们给点面试题和面试经验吧,谢谢了!!呵呵
- 请各位大虾帮忙:是关于eclipse开发ejb的问题
- 在线等:为何我的客户端EJBHome返回为空!~?
- 我的mysql版本为4.0.12的,我在struts中操作mysql数据库,报错no suitable driver,该怎样解决?谢谢!
- 求Effective Java电子中文书,分少可再加 ,急!!!!
- 小弟对于soap传输对象问题的疑惑,请指点!
- 请教高手,求设计思路,急。。
- spring邮件操作需要的JAR包
throws Exception {
File f = new File(fileName);
BufferedWriter bw = null;
try {
if (!f.exists()) {
if (!f.createNewFile()) {
throw new IOException("file create failure...");
}
}
bw = new BufferedWriter(new FileWriter(f, isAppend));
bw.write(str); } catch (Exception e) {
e.printStackTrace();
} finally {
try {
bw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
{
private String xml = "";
public XmlCreator()
{
this.xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
}
public XmlCreator(String Encoding)
{
this.xml = "<?xml version=\"1.0\" encoding=\"" + Encoding + "\"?>";
}
public void beginElement(String element)
{
this.xml += "<" + element + ">" ;
}
public void addAttribute(String attribute, String value)
{
this.xml = this.xml.substring(0,this.xml.length()-1) + " " + attribute + "='" + value + "'>";
}
public void endElement(String element)
{
this.xml += "</" + element + ">";
} public void addElement(String element, String content)
{
this.xml += "<" + element + ">" + content + "</" + element + ">";
}
public void addElement(String element, int content)
{
this.xml += "<" + element + ">" + String.valueOf(content) + "</" + element + ">";
}
public String getXml()
{
return this.xml;
}
}
HttpServletRequest request, HttpServletResponse response) throws IOException
{
String file = "";
if(request.getParameter("file") != null && request.getParameter("file").trim() != "")
{
file = request.getParameter("file"); //从参数取XML文件名
//组织XML文档路径
String filePath = request.getSession().getServletContext().getRealPath("/") + "WEB-INF/" + file;
FileInputStream fileinputstream = new FileInputStream(filePath);//读取XML文件内容
int lenght = fileinputstream.available();
byte bytes[] = new byte[lenght];
fileinputstream.read(bytes);
fileinputstream.close(); String content = new String(bytes); //取出构建好的XML文档
response.setCharacterEncoding("UTF-8");
response.setContentType("text/xml");
PrintWriter out = response.getWriter();
out.write(content);
out.flush();
out.close();
}
return null;
}
* 从xml脚本解析成Document对象
*
* @param xmlStr
* String
* @return Document
*/
public static Document str2Doc(String xmlStr) throws IOException,
JDOMException {
InputStream inputStream = new ByteArrayInputStream(xmlStr
.getBytes("UTF-8"));
SAXBuilder saxBuilder = new SAXBuilder();
Document doc = saxBuilder.build(inputStream);
return doc;
}