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

原创粉丝点击