Dom4j attribute value line breaks

Source: Internet
Author: User
Tags compact

The XML file was transferred yesterday and the XML file was dynamically generated using dom4j. It was found that the line breaks included in the original property value text were removed. The Code is as follows:

 

Private inputstream sendposttoservlet (string URL, document xmldoc,
String encodeing)
Throws exception
{
Inputstream result = NULL;
Try
{
// Set a URL Connection that can send content
URL httpurl = new URL (URL );
Httpurlconnection httpconn = (httpurlconnection) httpurl.
Openconnection ();
Httpconn. setdooutput (true );
Httpconn. setdoinput (true );
Httpconn. setconnecttimeout (10 );
// Set the sent content
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 ();
// Obtain the Server Response Information
Result = httpconn. getinputstream ();
}
Catch (exception E)
{
Throw E;
}

Return result;
}
I thought whether the carriage return was lost or processed during transmission. My colleague said it was impossible. Print the result directly to the console. The line break is no longer displayed. The problem is estimated to be in the format class. Check dom4j's outputformat description to find that the method for generating the format instance is wrong.

 


/**
Static Method to generate an instance for formatting the output, similar to opening XML in IE
* 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;
}

/**
* Static methods are provided to generate compact XML without branch
* 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;
}

/**
* Create an instance to completely retain the content of the element attribute text, and do not filter text content including carriage return and line breaks
* 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 encoded.
*
* @ Param indent
* Is the indent string to be used for indentation (usually
* Number of spaces ).
*/
Public outputformat (string indent ){
This. indent = indent;
}

/**
* Creates an <code> outputformat </code> with the given indent added
* Optional newlines between the elements. All whitespace from element text
* Will be removed ded.
*
* @ Param indent
* Is the indent string to be used for indentation (usually
* Number of spaces ).
* @ Param newlines
* Whether new lines are added to layout
*/
Public outputformat (string indent, Boolean newlines ){
This. indent = indent;
This. newlines = newlines;
}

/**
* Creates an <code> outputformat </code> with the given indent added
* Optional newlines between the elements and the given encoding format.
*
* @ Param indent
* Is the indent string to be used for indentation (usually
* Number of spaces ).
* @ Param newlines
* Whether new lines are added to layout
* @ 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;
}

 

Modify outputformat to generate an instance. The problem is solved.

// Set the sent content
Outputformat format = new outputformat ();
Xmlwriter writer = NULL;
Format. setencoding ("GBK ");
Writer = new xmlwriter (httpconn. getoutputstream (), format );
Writer. Write (xmldoc );
Writer. Close ();

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/hye4/archive/2008/11/05/3226940.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.