dom4j 属性值回车换行问题
来源:互联网 发布:淘宝中图片轮播的尺寸 编辑:程序博客网 时间:2024/04/30 03:36
昨天在做xml文件传输,利用dom4j动态生成xml文件,发现原先属性值文本包含的回车换行都被去掉了,代码如下:
private InputStream sendPostToServlet(String url, Document xmlDoc,
String encodeing)
throws Exception
{
InputStream result = null;
try
{
// 设置一个可以发送内容的URL连接
URL httpurl = new URL(url);
HttpURLConnection httpConn = (HttpURLConnection)httpurl.
openConnection();
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.setConnectTimeout(10);
// 设置发送的内容
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = null;
format.setEncoding("GBK");
writer = new XMLWriter(httpConn.getOutputStream(),format);
//Writer = new XMLWriter();
PrintWriter out = new PrintWriter(httpConn.getOutputStream());
//PrintWriter out = new PrintWriter(System.out);
Writer.setWriter(out);
writer.write(xmlDoc);
writer.close();
out.flush();
out.close();
// 获取服务器返回信息
result = httpConn.getInputStream();
}
catch (Exception e)
{
throw e;
}
return result;
}
原先以为是不是在传输的过程中会不会把回车换行丢失了或者处理掉了,同事说不可能。直接把结果往控制台打印看,果然已经没有回车换行了,问题估计出在format类上,查看dom4j关于OutputFormat说明,才发现format生成实例的方法是错的。
/**
静态方法,生成一个格式化输出的实例,类似IE打开xml
* A static helper method to create the default pretty printing format. This
* format consists of an indent of 2 spaces, newlines after each element and
* all other whitespace trimmed, and XMTML is false.
*
* @return DOCUMENT ME!
*/
public static OutputFormat createPrettyPrint() {
OutputFormat format = new OutputFormat();
format.setIndentSize(2);
format.setNewlines(true);
format.setTrimText(true);
format.setPadText(true);
return format;
}
/**
* 提供静态方法,生成紧凑型xml,不分行
* A static helper method to create the default compact format. This format
* does not have any indentation or newlines after an alement and all other
* whitespace trimmed
*
* @return DOCUMENT ME!
*/
public static OutputFormat createCompactFormat() {
OutputFormat format = new OutputFormat();
format.setIndent(false);
format.setNewlines(false);
format.setTrimText(true);
return format;
}
/**
*创建实例,完全保持element属性文本内容,对文本内容包含回车换行不过滤
* Creates an <code>OutputFormat</code> with no additional whitespace
* (indent or new lines) added. The whitespace from the element text content
* is fully preserved.
*/
public OutputFormat() {
}
/**
* Creates an <code>OutputFormat</code> with the given indent added but no
* new lines added. All whitespace from element text will be included.
*
* @param indent
* is the indent string to be used for indentation (usually a
* number of spaces).
*/
public OutputFormat(String indent) {
this.indent = indent;
}
/**
* Creates an <code>OutputFormat</code> with the given indent added with
* optional newlines between the Elements. All whitespace from element text
* will be included.
*
* @param indent
* is the indent string to be used for indentation (usually a
* number of spaces).
* @param newlines
* whether new lines are added to layout the
*/
public OutputFormat(String indent, boolean newlines) {
this.indent = indent;
this.newlines = newlines;
}
/**
* Creates an <code>OutputFormat</code> with the given indent added with
* optional newlines between the Elements and the given encoding format.
*
* @param indent
* is the indent string to be used for indentation (usually a
* number of spaces).
* @param newlines
* whether new lines are added to layout the
* @param encoding
* is the text encoding to use for writing the XML
*/
public OutputFormat(String indent, boolean newlines, String encoding) {
this.indent = indent;
this.newlines = newlines;
this.encoding = encoding;
}
修改OutputFormat 生成实例方法,问题解决。
// 设置发送的内容
OutputFormat format = new OutputFormat();
XMLWriter writer = null;
format.setEncoding("GBK");
writer = new XMLWriter(httpConn.getOutputStream(),format);
writer.write(xmlDoc);
writer.close();
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/hye4/archive/2008/11/05/3226940.aspx
- dom4j 属性值回车换行问题
- 回车换行问题
- FCKEditor回车换行问题
- UltraEdit回车换行问题
- 记事本中的回车换行问题
- 关于回车换行的问题
- 奇怪的回车换行问题
- java中的回车换行问题
- vim中回车换行缩进问题
- 不同操作系统回车换行问题总结
- Beyond Compare 关于回车换行的问题
- goto语句遇到的问题 回车/换行
- [XAML]回车换行空格等问题
- 回车换行
- 回车 换行
- 回车换行
- 回车换行
- 回车换行
- 兄弟
- 六一儿童节的礼物:基于GB18030-2000汉字拼音类库(C#)
- 终于好了!!
- 跟我一起写 Makefile
- 用dom4j解析xml文件写入记事本时,解决换行问题
- dom4j 属性值回车换行问题
- 拉斯维加斯之n皇后问题
- 用C++来实现MD5的生成~~
- php带有数据库的二级联动菜单
- c++MD5加密类
- 海豹突击队给企业家的五条管理启示
- .NET安全系列之一:代码访问安全(CAS)
- Linux上vi(vim)编辑器使用教程
- Linux上vi(vim)编辑器使用教程