dom4j 屬性值斷行符號換行問題

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.