1. 程式人生 > >dom4j 屬性值回車換行問題

dom4j 屬性值回車換行問題

   昨天在做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();